A NativeScript module providing sqlite actions for Android and (soon) iOS.
Run npm install nativescript-sqlite --save
from your project's inner app
directory:
.
├── app
│  ├── app <------------------------------ run npm install from here
│  │  ├── app.css
│  │  ├── app.js
│  │  ├── bootstrap.js
│  │  ├── main-page.js
│  │  ├── main-page.xml
│  │  ├── node_modules
│  │  │  └── nativescript-sqlite <-- The install will place the module's code here
│  │  │  └── ...
│  │  └── package.json <----------------- The install will register “nativescript-sqlite” as a dependency here
│  └── tns_modules
│  └── ...
└── platforms
├── android
└── ios
As is, using npm within NativeScript is still experimental, so it's possible that you'll run into some issues. A more complete solution is in the works, and you can check out this issue for an update on its progress and to offer feedback.
If npm doesn't end up working for you, you can just copy and paste this repo's sqlite.android.js, and sqlite.ios.js files into your app and reference them directly.
To use the sqlite module you must first require()
it from your project's node_modules
directory:
var Sqlite = require( "./node_modules/nativescript-sqlite/sqlite" );
After you have a reference to the module you can then call the available methods. The database defaults to returning result sets in arrays; i.e. [[field1, field2, ...], [field1, field2], [field1, field2] ...] you can change this to returning them in objects if you desire.
- All callbacks have the standard (Error, result) prototype
- Sqlite.RESULTSASARRAY - Returns results as Arrays (ex: select name, phone --- results [[name,phone]])
- Sqlite.RESULTSASOBJECT - Returns results as Objects (ex: select name, phone --- results [{name: name, phone: phone}]
- dbname: your database name.
- (optional) callback (error, db): db is the fully OPEN database object that allows interacting with the db.
- RETURNS: promise of the DB object
// my-page.js
var Sqlite = require( "/path/to/node_modules/nativescript-sqlite" );
var db_promise = new Sqlite("MyTable", function(err, db) {
// This should ALWAYS be true, db object is open in the "Callback"
console.log("Are we open yet (Inside Callback)? ", db.isOpen() ? "Yes" : "No"); // Yes
});
db_promise.then(function(db) {
// This should ALWAYS be true, db object is open in the "then"
console.log("Are we open yet (Inside Promise)? ", db.isOpen() ? "Yes" : "No"); // Yes
db.close();
}, function(err) {
console.error("We failed to open database", err);
});
- obj
- Returns True or False if the obj passed to this function is a
// my-page.js
new Sqlite("test.db", function(err, db) {
console.log("Is a Sqlite Database:", Sqlite.isSqlite(db) ? "Yes" : "No); // Should print "Yes"
});
- Callback_OR_Version to set it to If Callback Value will have the version. On a new Database it will be Zero If Version number, then the database will be changed to the version you passed to this function
new Sqlite("test.db", function(err, db) {
db.version(function(err, ver) {
if (ver === 0) {
db.execSQL("Create table....");
db.version(1); // Sets the version to 1
}
});
};
- Is the current database open true/false
- Pass in Sqlite.RESULTASOBJECT or Sqlite.RESULTSASARRAY to change the result sets configuration This will set the database to return the results in which ever choice you make. (Default is RESULTSASARRAY)
- Closes the database NOTE: Any DB calls after this will throw errors.
DB.execSQL
< A1A3 /div>- SQL statement to run, can use ? for Parameters
- Params (Optional) - an array of Parameters
- Callback will either return null or the last id inserted or the record count of update/delete This routine you can use for "update", "insert", "delete" and any other sqlite command where you are not expecting a result set back. If this is a Insert it will return the last row id of the new inserted record. If it is a update/insert it will return the number of rows affected.
// new SQLite(....
db.execSQL("insert into Hello (word) values (?)", ["Hi"], function(err, id) {
console.log("The new record id is:", id);
});
- SQL SELECT statement, can use ? for parameters
- Params (Optional)
- Callback will have the first row of the result set.
// new SQLite(...
db.get('select * from Hello', function(err, row) {
console.log("Row of data was: ", row); // Prints [["Field1", "Field2",...]]
});
- SQL SELECT statement, can use ? for parameters
- Params (Optional)
- Callback will have the all the rows of the result set.
// new SQLite(...
db.all('select * from Hello', function(err, resultSet) {
console.log("Result set is:", resultSet); // Prints [["Row_1 Field_1" "Row_1 Field_2",...], ["Row 2"...], ...]
});
- SQL Select statement, can use ? for parameters
- Params (Optional)
- Callback will be called for EACH row of the result set with the current row value
- Finished_Callback (Optional) will be called when it is complete with the number of rows handled.