How to use SQLite in NimbleKit

Forum for posting sample code and examples, new posts creation is not allowed, but everybody can comment

How to use SQLite in NimbleKit

Postby Big-O » Fri Apr 09, 2010 7:02 am

As of NimbleKit 1.7 we again have built-in support for working with SQLite databases! It's easy to set up, easy to use, and best of all, very very fast.

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!
I am a small and fragile flower.

-- Big-O
User avatar
Big-O
NimbleKit Expert
 
Posts: 310
Joined: Thu Sep 24, 2009 4:02 am

Re: How to use SQLite in NimbleKit

Postby kunicki » Sun Apr 11, 2010 7:20 am

Can you qualify what you mean by "Very fast". SQLite is fast by itself, but why is your implementation so fast? Are you doing something special here to improve performance on the iPhone?

Tnx
kunicki
 
Posts: 58
Joined: Mon Sep 07, 2009 10:49 am

Re: How to use SQLite in NimbleKit

Postby Big-O » Mon Apr 12, 2010 11:07 pm

kunicki wrote:Can you qualify what you mean by "Very fast". SQLite is fast by itself, but why is your implementation so fast? Are you doing something special here to improve performance on the iPhone?

Tnx

Not at all. I was trying to say that using SQLite is going to be much faster than various other methods like working with text files or large javascript arrays, etc.
I am a small and fragile flower.

-- Big-O
User avatar
Big-O
NimbleKit Expert
 
Posts: 310
Joined: Thu Sep 24, 2009 4:02 am

Re: How to use SQLite in NimbleKit

Postby rouillip » Tue Apr 13, 2010 3:33 pm

I appreciate your help in usung SQLite in Nimblekit.
I have a problem as i try to add a record to my database
My database name : membre.sqlite
my table is client :
pk int (primary key)
nom varchar
prenom varchar
age int
email varchar

in a form (add_form.html) i enter my datas wich are stocked in variable as NKSetting before to goto to a new page (add.html)
In add.html i got back my 4 parameters and can display it.
Then i try to insert the record in my database

var database = new NKSQLite();
database.openDatabase("membre.sqlite", "no"); // use "yes" if you want to reset database to the default, generally you should always use "no"
database.executeSQL("INSERT INTO client(nom,prenom,age,email) VALUES(nom,prenom,age,email)");
database.closeDatabase();

and here, nothing happends, my database is not updated.

Is it something wrong in my syntax ?
I should say that i can read and displays my database in a list.html file
Thanks for your help
NimbleKit Registered user U885740601
rouillip
 
Posts: 71
Joined: Wed Jan 13, 2010 9:38 am
Location: Paris 75018 (France)

Re: How to use SQLite in NimbleKit

Postby bsoft » Tue Apr 13, 2010 4:19 pm

Nice tuto Big-O :)

Did you (or someone else) try to compare the sqlite solution to the sqlite embed in HTML5 ?
Is sqlite faster for reading, writing.. ?
bsoft
 
Posts: 100
Joined: Mon Jun 29, 2009 9:54 am

Re: How to use SQLite in NimbleKit

Postby tvalleau » Tue Apr 13, 2010 5:50 pm

rouillip wrote: snip

and here, nothing happends, my database is not updated.
Is it something wrong in my syntax ?


see here:

http://www.nimblekit.com/forum/viewtopic.php?f=6&t=1013
"Don't believe everything you think."
(coding for iPad with 1.8.3)
User avatar
tvalleau
NimbleKit Expert
 
Posts: 219
Joined: Tue Jan 05, 2010 7:58 pm

Re: How to use SQLite in NimbleKit

Postby sunny » Tue Apr 13, 2010 8:48 pm

@bsoft: yes, it's faster and allows to have pre-filled content in the database
User avatar
sunny
Staff
 
Posts: 1787
Joined: Sat May 30, 2009 5:18 am
Location: kiev, Ukraine

Re: How to use SQLite in NimbleKit

Postby rouillip » Wed Apr 14, 2010 7:06 pm

Hi Tvalleau

I try to use your trik

viewtopic.php?f=6&t=1013

For me it doesn't work.
I can read and display my database, but impossible to insert and update !
NimbleKit Registered user U885740601
rouillip
 
Posts: 71
Joined: Wed Jan 13, 2010 9:38 am
Location: Paris 75018 (France)

Re: How to use SQLite in NimbleKit

Postby tvalleau » Wed Apr 14, 2010 7:54 pm

then you are doing something wrong in your calls. The most common reason would be that you have opened the database incorrectly.

it needs to be
database.openDatabase("controldbx.rdb",0); (false)
not
database.openDatabase("controldbx.rdb",1); (true)

Why not post your code here, so we can look at it?
"Don't believe everything you think."
(coding for iPad with 1.8.3)
User avatar
tvalleau
NimbleKit Expert
 
Posts: 219
Joined: Tue Jan 05, 2010 7:58 pm

Re: How to use SQLite in NimbleKit

Postby rouillip » Wed Apr 14, 2010 10:33 pm

Hi,
Nice idea to send you a post of my application.

Infortunatly my application is 1.4 mo, far over the limit authorized by the forum (256 Ko), and my upload is refused.
Is it an email adress where i can send you my post ?
NimbleKit Registered user U885740601
rouillip
 
Posts: 71
Joined: Wed Jan 13, 2010 9:38 am
Location: Paris 75018 (France)

Next

Return to Sample Code

Who is online

Users browsing this forum: No registered users and 1 guest