Tuesday, December 25, 2012

BlackBerry SQLITE databse example with CURD Operations

1)Create a project with name DataBaseExample

and insert the code inside MyScreen.java


package mypackage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public final class MyScreen extends MainScreen {

public static final String uripath = "file:///SDCard/Databases/example/"
+ "DemoApp.db";

public MyScreen() {

setTitle("Database Example");
DatabaseSupporter dboperation = new DatabaseSupporter();
dboperation.createDataBaseApp(uripath);
add(new LabelField("database created"));
add(new SeparatorField());
dboperation.createDataBaseTable(uripath);
add(new LabelField("database table created"));
add(new SeparatorField());
dboperation.insertintoDataBaseTable(uripath);
add(new LabelField("database value inserted"));
add(new SeparatorField());
String temp = dboperation.retrievefromoDataBaseTable(uripath);
add(new LabelField("database retrieved==" + temp));
add(new SeparatorField());
//dboperation.updateDataBaseTable(uripath);
//dboperation.deleteDatabase(uripath);
}
}





2) now make new java class for CURD operations as DatabaseSupporter.java and copy following code to it



package mypackage;

import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.io.URI;

public class DatabaseSupporter {
Database d;

public DatabaseSupporter() {
}

public void createDataBaseApp(String uripath) {

try {
URI myURI = URI.create(uripath);
if (DatabaseFactory.exists(myURI) == true) {
// nothing to do if ALREADY exists
} else {
d = DatabaseFactory.create(myURI);
d.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

}

public void createDataBaseTable(String uripath) {
try {
URI myURI = URI.create(uripath);
d = DatabaseFactory.open(myURI);
Statement st_todelete = d
.createStatement("DROP TABLE if exists People");
st_todelete.prepare();
st_todelete.execute();
st_todelete.close();
Statement st = d.createStatement("CREATE TABLE 'People' ( "
+ "'Name' TEXT, " + "'Age' INTEGER )");
st.prepare();
st.execute();
st.close();
d.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

}

public void insertintoDataBaseTable(String uripath) {
try {
URI myURI = URI.create(uripath);
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("INSERT INTO People(Name,Age) "
+ "VALUES ('Jitesh',26)");
st.prepare();
st.execute();
st.close();
d.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

}

public String retrievefromoDataBaseTable(String uripath) {
String stringvalue = "value inside db is==>";
try {
URI myURI = URI.create(uripath);
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("SELECT Name,Age FROM People");
st.prepare();
net.rim.device.api.database.Cursor c = st.getCursor();
Row r;
int i = 0;
while (c.next()) {
r = c.getRow();
i++;
stringvalue = stringvalue
+ (" " + i + "=> Name = " + r.getString(0) + " , "
+ "Age = " + r.getInteger(1));
}
if (i == 0) {
return "there is nothing to return";
}
st.close();
d.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return stringvalue;
}

public void updateDataBaseTable(String uripath) {
try {
d = DatabaseFactory.open(uripath);
Statement st = d.createStatement("UPDATE People SET Age=27 "
+ "WHERE Name='jitesh'");
st.prepare();
st.execute();
st.close();
d.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

public void deleteDatabase(String uripath) {
try {
URI myURI = URI.create(uripath);
DatabaseFactory.delete(myURI);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

}

}

3) run on emulator, do not forget to insert sd card, follow these steps

simulate==>change sd card=> [click on "+ folder icon" and choose the path]=> now click on "+" at right side to insert sd card and click again on the path value shown to you



No comments:

Post a Comment