Wednesday, November 2, 2011

Storing Data's Using SQLite Database -- Blackberry


Data storage in Blackberry includes several methods like Persistent Store, RMS and using SQ Lite Database. Now we are going to see about storing and retrieving values in SQ Lite Database.


CODE


Creating Database in SD Card :

Public void CreateDatabase()
{
Database d;

try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db");
d = DatabaseFactory.create(myURI);
d.close();
add(new RichTextField("DB created successfully"));
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}
}


Creating Table in Database:

Public void CreateTable()
{
Database d;
try
{
URI myURI = URI.create("file:///SDCard/Databases/"+"Test.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement( "CREATE TABLE 'testtable' ( " +"'id' INTEGER, " +"'name' TEXT ");

st.prepare();
st.execute();
st.close();
d.close();
add(new RichTextField("Table created successfully"));
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}

}

Inserting data’s in Table:

Public void InsertData ()
{
Database d;
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db");
d = DatabaseFactory.open(myURI);

Statement st = d.createStatement("INSERT INTO testtable(id,name) " + "VALUES (1,’Arun’)");
st.prepare();
st.execute();
st.close();
d.close();
add(new RichTextField("Values Inserted"));
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}

}


Retrieving Values from Database:

Public void RetriveValues()
{
Database d;

try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("SELECT id,name FROM testtable");
st.prepare();
net.rim.device.api.database.Cursor c = st.getCursor();

Row r;
int i = 0;
while(c.next())
{
r = c.getRow();
i++;
add(new RichTextField(i + ". ID = " + r.getInteger(0)
+ " , "
+". Name = " + r.getString(1)));
}
if (i==0)
{
add(new RichTextField("No data in the table."));
}
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}
}


You can download the sample project below.



Arun Kumar Munusamy Web Developer

Morbi aliquam fringilla nisl. Pellentesque eleifend condimentum tellus, vel vulputate tortor malesuada sit amet. Aliquam vel vestibulum metus. Aenean ut mi aucto.

3 comments:

  1. I want to make DBAdapter class have all method that need like openOrCreate,insert and delete but it doesn't work can you help me

    ReplyDelete
  2. Hi, i tried the above code in the device's internal memory having path as "file:///store/home/user/Database/Test.db", it created the database folder under user folder successfully but it failed to create Test.db and throwing exception as File System not ready. I don't know much about Blackberry, so can you please help me where i am lacking, which can help me a lot. I have also given all the permissions to the application so can there be any issue regarding this.Thank you.

    ReplyDelete
  3. Controlled Access Exception is occurring while running in the simulator

    ReplyDelete