Creating a SQLite database-
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.create(myURI);
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Creating Table -
try
{
URI myURI = URI.create("/SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement( "CREATE TABLE 'MyTable' ( " +
"'Name' TEXT, " +
"'Number' INTEGER )" );
st.prepare();
st.execute();
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Insert Data -
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("INSERT INTO MyTable(Name,Number) " +
"VALUES ('abc',123)");
st.prepare();
st.execute();
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Retrive data -
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("SELECT Name,Number FROM MyTable");
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 + ". Name = " + r.getString(0) +
" , " +
"Number= " + r.getInteger(1)));
}
if (i==0)
{
add(new RichTextField("No data in the MyTable table."));
}
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Delete Table -
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("DELETE MyTable");
st.prepare();
st.execute();
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Update Table -
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("UPDATE MyTable SET Number=1" +
"WHERE Name='abc'");
st.prepare();
st.execute();
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Delete Database -
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +
"MyDatabase.db");
DatabaseFactory.delete(myURI);
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
No comments:
Post a Comment