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.
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
ReplyDeleteHi, 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.
ReplyDeleteControlled Access Exception is occurring while running in the simulator
ReplyDelete