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/

  • 相关阅读:
    JSON跨域请求
    2013.9.26 心得体会
    MemCached用法
    使用SQL联合查询来构建临时vo对象的应用
    ubuntu 16.04 安装php 5 6等版本
    mac php版本切换
    windows 查看端口占用
    nginx 反向代理到目录
    linux挂在samba服务器到本地(用于备份文件到nas或者windows的文件服务器)
    ubuntu 加载新硬盘或分区
  • 原文地址:https://www.cnblogs.com/isdom/p/webclips058.html
Copyright © 2011-2022 走看看