NKSQLite
Overview
This class is used to access database and use sqlite databases. NKSQLite is global singleton class so you can access opened database from any page, but if you open new one, previous will be closed automatically. So itʼs preferable to use 1 database with multiple tables to open/close database only once.
openDatabase(name, rewrite)
opens database with given name and rewrites it if "rewrite" is set to "1" or "yes".
Discussion:
SQLite database file is stored in application bundle, but the location is not writable so
when we call openDatabase NimbleKit tries to open database with this name from
writable location in documents folder of iPhone, it there is no such file - NimbleKit
copies it from bundle to writable location and opens again. So once you opened your
database on current device, database file in bundle wonʼt be accessed, but of course if
you changed tables or put new data in initial table you can put rewrite variable to "1" or
"yes" to rewrite database at writable location.
executeSQL(query)
executes standard SQL query, (i.e. "SELECT * FROM mytable")
getResults()
returns the result as javascript array of dictionaries
closeDatabase()
closes current database
Example:
var database = new NKSQLite();
database.openDatabase("nk.sqlite", "no");
database.executeSQL("CREATE TABLE todo(pk INTEGER PRIMARY KEY, text VARCHAR(25), priority INTEGER, complete
BOOLEAN)");
database.executeSQL("INSERT INTO todo(text,priority,complete) VALUES('Take out the trash',3,0)");
database.executeSQL("INSERT INTO todo(text,priority,complete) VALUES('Do Computer Science homework',1,0)");
database.executeSQL("INSERT INTO todo(text,priority,complete) VALUES('Learn Objective C',1,0)");
database.executeSQL("INSERT INTO todo(text,priority,complete) VALUES('DIGG this tutorial',2,0)");
database.executeSQL("SELECT * FROM todo");
results = database.getResults();
for (i=0; i<results.length; i++)
{
! document.body.innerHTML+="<p>"+results[i].priority+" | "+results[i].complete+" | "+results[i].text+"</p>";
}
database.closeDatabase();

