ionic sqlite表的一些操作,ionicsqlite表
var db = null;
$ionicPlatform.ready(function() { db = $cordovaSQLite.openDB({//创建数据库 name: "wpf.db", location: 1 }); wpf.setCordovaSQLite($cordovaSQLite); wpf.getSQLiteClass(db).createTable(['wpf']);//创建表 });
var wpf = function {
var setCordovaSQLite = function (obj) { $cordovaSQLite = obj; }
var getSQLiteClass = function (db) { var patterns = { wpf : { 'userId': 'text PRIMARY KEY', 'username':'text', 'password': 'text' } } return { createTable: function (tables) { for (var i in tables) { var table = tables[i]; var query = "CREATE TABLE IF NOT EXISTS `#table#` (#cols#)".replace('#table#', table); var pattern = patterns[table]; var cols = []; for (var key in pattern) { cols.push(key + " " + pattern[key]); } query = query.replace('#cols#', cols.join(',')); console.log(query); $cordovaSQLite.execute(db, query); } }, saveRecords: function (table, records, callback) { var query = "REPLACE INTO `#table#` (#cols#) VALUES #rows#".replace('#table#', table); var pattern = patterns[table]; var cols = [], rows = [], _rows = ''; var dbInterval = null; for (key in pattern) { cols.push(key); } if (records instanceof Array) { for (var i in records) { var row = []; for (key in pattern) { var val = (records[i][key] || pattern[key]); //val= val.replace(/'/g,"\\'"); row.push(val === null ? "null" : "'" + val + "'"); } rows.push("(#row#)".replace("#row#", row.join(','))); } _rows = rows.join(','); } else { for (var k in cols) { rows.push((records[cols[k]] === null || records[cols[k]] == undefined) ? "null" : "'" + records[cols[k]].toString().replace(/'/g, "’") + "'"); } _rows = "(" + rows.join(',') + ")"; } query = query.replace('#cols#', cols.join(',')).replace('#rows#', _rows); console.log(query); if (!window.db) { dbInterval = $interval(function () { if (window.db) { doSaveCord(); } }, 200, 15); } else { doSaveCord(); } function doSaveCord() { $cordovaSQLite.execute(window.db, query).then(function (res) { callback && callback(res); }, function (err) { console.error(err); }); if (dbInterval) { $interval.cancel(dbInterval); } } }, selectRecords: function (table, where, callback) { var query = "SELECT * FROM `#table#` #where#".replace('#table#', table).replace('#where#', where || ''); var dbInterval = null; console.log(query); if (!window.db) { dbInterval = $interval(function () { if (window.db) { doSelectCord(); } }, 200, 15); } else { doSelectCord(); } function doSelectCord() { $cordovaSQLite.execute(window.db, query).then(function (res) { callback && callback(res); }, function (err) { console.error(table + " " + JSON.stringify(err)); }); if (dbInterval) { $interval.cancel(dbInterval); } } }, deleteRecords: function (table, where, callback) { var dbInterval = null; var query = "DELETE FROM `#table#` #where#".replace('#table#', table).replace('#where#', where || ''); console.log(query); if (!window.db) { dbInterval = $interval(function () { if (window.db) { doDeleteCord(); } }, 200, 15); } else { doDeleteCord(); } function doDeleteCord() { $cordovaSQLite.execute(window.db, query).then(function (res) { callback && callback(res); }, function (err) { console.error(err); }); if (dbInterval) { $interval.cancel(dbInterval); } } } } }
}
//使用
var params = { userId: $scope.loginData.username, username: $scope.loginData.username, password: $scope.loginData.password }; wpf.getSQLiteClass(db).saveRecords('wpf', params, function () { console.log("wpf table saves success"); zzExercitation.getSQLiteClass(db).selectRecords('wpf', null, function (res) { console.log("===============wpf start==================="); console.log(res); console.log("===============wpf end==================="); }); });
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。