First, get yourself a SQLite database. There are lots of ways to do that, and a few Google searches will turn up plenty of free management tools. I personally love the "SQLite Manager" add-on for Firefox:
https://addons.mozilla.org/en-US/firefox/addon/5817
Next, import the database into xcode. You can just drag and drop from the finder into the "groups and files" window in xcode when you have your project open. To make sure that the db actually gets deployed with the app, look at the groups and files window, under Targets, then "Copy Bundle Resources". Your db should appear under that list or it won't get included when the app is deployed. If it's not there, look under Targets, then "Compile Sources" and if you find it there, then drag it over to the copy bundle resources folder.
Now that that's done, let's set up the db and open it:
- Code: Select all
//create the object that you will work with for the db
var database = new NKSQLite();
//now open it up and specify if you want the existing db along with any
//data the user may have entered to be overwritten with your db file from the app
database.openDatabase("myDataFile.sqlite", "no");
//now just run your sql query
database.executeSQL("SELECT * from myTable");
//let's make an array and store the data rows in it
var myRows = new Array();
myRows = database.getResults();
Once you've done that, you now have an array and each element of that array represents one row of data returned by your sql query. Next we'll loop through the rows and do something with the data, and I'll show you how NimbleKit takes it one step farther to make it easy to work with:
- Code: Select all
//loop through the array
for (i=0;i<myRows.length;i++)
{
//NimbleKit automatically extends the array to make each column easy to access
//let's write the value of a column called "myColumnName" to the page
document.write(myRows[i].myColumnName + "<br />");
}
database.closeDatabase();
And that's all there is to it! Just be sure to use things like the NKActivityIndicator when you are working with data, such as running a query or parsing the data. SQLite is pretty darn fast, but performance on-device can vary. I've used this with a data set as large as 41,000 records in NimbleKit with SQLite and it still works fine, although it takes a few seconds for it to complete at that point.
Good luck!