当前位置:首页 > 数据库 > SQlite

Use SQLite Instead of Local Storage In Ionic Framework【转】

example.controller("ExampleController", function($scope, $cordovaSQLite) { 2 3 $scope.insert = function(firstname, lastname) { 4 var query = "INSERT INTO people (firstname, lastname) VALUES (?,?)"; 5 $cordovaSQLite.execute(db, query, [firstname, lastname]).then(function(res) { 6 console.log("INSERT ID -> " + res.insertId); 7 }, function (err) { 8 console.error(err); 9 }); 10 } 11 12 $scope.select = function(lastname) { 13 var query = "SELECT firstname, lastname FROM people WHERE lastname = ?"; 14 $cordovaSQLite.execute(db, query, [lastname]).then(function(res) { 15 if(res.rows.length > 0) { 16 console.log("SELECTED -> " + res.rows.item(0).firstname + " " + res.rows.item(0).lastname); 17 } else { 18 console.log("No results found"); 19 } 20 }, function (err) { 21 console.error(err); 22 }); 23 } 24 25 });

I’ve gone ahead and created two very basic functions.  The insert function will add a first and last name record into the database while the select function will find records by last name.  Basic, but I hope you get the idea.

Something to note about my controller though.  I am not doing these database calls from inside an onDeviceReady() function.  Had these functions been fired when the controller loads, they probably would have failed.  Since I am basing the database activity off button clicks, it is probably safe to assume the device and database is ready for use.

If you wish to delete any of your SQLite databases you can do so by including the following:

1 $cordovaSQLite.deleteDB("my.db");

 

注: 本文系统来源:http://www.cnblogs.com/dreamofei/p/4806491.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!