Wrote a module, the application of the Sqllite3 database, the upgrade principle is to check the version of the database version of the values in the table, the version of the value of one to read the corresponding SQL file, execute the SQL statement, and then loop to say to remove a file until the completion.
All SQL files in this module are placed under the/sql/folder, such as: Contact_create.sql,contact_update_1.sql,contact_update_2.sql, etc., executed when used the Updateuserdb () method is OK.
Var _=require ("underscore"); Var sqlite3 = require (' Sqlite3 '). verbose (); var fs = require (' FS '); Var async = require ("async"); Var path=require ("path"); Environmentdao = function () {var db={},dbnames=["contact", "groups", "message"];//for example //Initialize Var init = function () { _.each (dbnames,function (name) {DB[name] =new sqlite3. Database (Path.join ("/main/db", name+ '. db '), sqlite3. Open_readwrite | sqlite3. open_create) }; Init ();// checks if the database is empty this.checkdb = function (db,callback) {db.get ("select count (*) as exist from sqlite_master where type= ' table ' and name= ' version ', function ( Err,result) {if (err) {console.error ("Checkdb error", err); callback (Null,false);} Else{if (result.exist == 1) {callback (null,true);} Else{callback (Null,false);}});};/ / Universal Initialization Database this.initdbcommon = function (Db,dbname,callback) {var self=this;var sql = fs.readfilesync ('/sql/' + Dbname+ "_create.sql", ' UTF8 ');d b.exec (sql,function (Err,result) {if (err) {Console.error ("create db error ", Dbname,err)}callback&&callback (Err,result)});};/ / Universal Get Database version This.getdbversioncommon = function (db,callback) {db.get ("select version from version where key = ' db_version ', function (Err,result) {if (err) {Console.error (" Getdbversion err ", err);} Callback&&callback (Err,result);})};/ / Universal Upgrade Database This.updatedbcommon = function (db,dbname,callback) { var self = this;var doupdate = function (CB) {Self.getdbversioncommon (Db,function (ERR,DBV) {if (err| |! DBV) {console.error ("getdbversion err ", dbname,dbv,err);//re-create table without table or table name error-Fault tolerant mechanism if (err.code == "Sqlite_error") {Self.initdbcommon (Db,dbname,function (Err) {if (!err) {Self.updatedbcommon (Db,dbname, functionERR) { if (err) { Console.error ("Second time init and update db error", DbName,err);       }        CB (True); });}    &NBSP})}else{//Other errors ignore CB (FALSE)}}else{var v = dbv.version;v++; if (Fs.existssync ('/sql/' +dbname+ "_update_" +v+ '. sql ')) {var sql = fs.readfilesync ('/sql/' +dbname+ "_update_" +v+ '. sql ', ' UTF8 '); db.exec (SQL, function (Err) { if (err) { console.error ("Updatedbcommon err:" , Dbname,sql,err);     CB (false);     }ELSE{    CB ( True); } }); }else{  CB (false); }});} DoUpdate (function (tryagain) {if (Tryagain) {doupdate.apply (This,[arguments.callee]);} Else{if (typeof callback == "function") callback ();});};/ / Upgrade all user Databases This.updateuserdb=function (callback) {Var self = this;async.foreach ( Dbnames, function (DBNAME,CB) { self.checkdb (db[dbname],function (err,isExist) { if (isexist) { self.updatedbcommon (DB[DBNAME],DBNAME,CB); }else{ self.initdbcommon (Db[dbname],dbname,function () { self.updatedbcommon (DB[DBNAME],DBNAME,CB); }) } }) }, function (err) { if (err) { &nbsP; logger.error ("Updateuserdb error:", err); } if (typeof callback == "function") {callback (err);} });}; Exports = module.exports = new environmentdao ();
Database upgrade mechanism in Node-webkit