zoukankan      html  css  js  c++  java
  • nodejs与sqlite

    //打开数据库
    var db = new sqlite3.Database('xx.db');

    // 关闭数据库
    db.close();

    db.run('xx');  // 数据库对象的run函数可以执行任何的SQL语句,该函数一般不用来执行查询
                   // create alter 之类的


    增:
    var stmt = db.prepare("INSERT OR REPLACE INTO note (cdate, content) VALUES (?,?)");
    stmt.run(data.cdate, data.content);
    stmt.finalize();

    删:
    db.prepare("DELETE  from note where cdate =?");  
    stmt.run(data.cdate);  
    stmt.finalize();

    改:
    var stmt = db.prepare("UPDATE note set content=? where cdate =?");  
    stmt.run(data.content, data.cdate);  
    stmt.finalize();  

    查:
    db.each("SELECT rowid AS id, thing FROM Stuff", function(err, row) {
        console.log(row.id + ": " + row.thing);
      });
    });
    // or
    db.all("SELECT xxx", function (err, res){});

     // 使用

    先把库下载到node_modules  npm install sqlite3 --save


     1.引入sqlite3库
    var sqlite3 = require('sqlite3');
    // or  var sqlite3 = require("sqlite3").verbose();

    var db = new sqlite3.Database(file);
    db.serialize(function() {
         //Do stuff...
         db.run("CREATE TABLE Stuff (thing TEXT)");

         var stmt = db.prepare("INSERT INTO Stuff VALUES (?)");
         for(var i = 0;i<xx;i++){
             stmt.run('xx');
         }     
         stmt.finalize();

    });
    db.close();

    nodejs 与sqlite
    http://blog.modulus.io/nodejs-and-sqlite
    http://book.51cto.com/art/201504/473574.htm

    http://www.cnblogs.com/EricaMIN1987_IT/p/3654826.html

    https://www.sqlite.org/

  • 相关阅读:
    nginx:安装成windows服务
    org.aspectj.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18
    数据库中间件
    架构策略
    谈判
    设计模式 总结 常用10种
    08 状态模式 state
    07 策略模式 strategy
    06 命令模式(不用)
    05 观察者模式 Observer
  • 原文地址:https://www.cnblogs.com/isdom/p/webclips058.html
Copyright © 2011-2022 走看看