当前位置:Gxlcms > 数据库问题 > Use SQLite Instead of Local Storage In Ionic Framework【转】

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

时间:2021-07-01 10:21:17 帮助过:7人阅读

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");

 

注:

人气教程排行