Difference between revisions of "Teams Winter 2011/team1/BlackBerry/Use SQLite"
Ladanzahir (talk | contribs) (→11.1 Add SDCard to Simulator) |
Ladanzahir (talk | contribs) (→11.2 Create Database File) |
||
Line 7: | Line 7: | ||
===11.2 Create Database File=== | ===11.2 Create Database File=== | ||
+ | We would like to create a database file including out Student Entity, so that we can put it in the project. Then every time the app runs, first it checks if it is the first time run of the app on the device or simulator. If so, the app will copy the file in to the SDCard. Next time that you run the application, it will know that the file is there, so will not override it. <Br/> | ||
+ | |||
+ | For this purpose, create a simple blackberry project with only the application object. We do not need any screen here. call it "DBCreator". | ||
+ | and this will need to only have a class called MyApp.java which extends UIApplication. Here is the code for this class: | ||
+ | |||
+ | <source lang="java" > | ||
+ | |||
+ | package mypackage; | ||
+ | |||
+ | import java.util.Enumeration; | ||
+ | import javax.microedition.io.file.FileSystemRegistry; | ||
+ | import net.rim.device.api.ui.*; | ||
+ | import net.rim.device.api.ui.component.*; | ||
+ | import net.rim.device.api.database.*; | ||
+ | import net.rim.device.api.io.*; | ||
+ | |||
+ | /** | ||
+ | * This class extends the UiApplication class, providing a | ||
+ | * graphical user interface. | ||
+ | */ | ||
+ | public class MyApp extends UiApplication | ||
+ | { | ||
+ | private static String DB_NAME = "SQLiteStudentList"; | ||
+ | |||
+ | /** | ||
+ | * Entry point for application | ||
+ | * @param args Command line arguments (not used) | ||
+ | */ | ||
+ | public static void main(String[] args) throws Exception | ||
+ | { | ||
+ | // Create a new instance of the application and make the currently | ||
+ | // running thread the application's event dispatch thread. | ||
+ | MyApp theApp = new MyApp(); | ||
+ | theApp.enterEventDispatcher(); | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Creates a new MyApp object | ||
+ | */ | ||
+ | public MyApp()throws Exception | ||
+ | { | ||
+ | // Push a screen onto the UI stack for rendering. | ||
+ | boolean sdCardPresent = false; | ||
+ | String root = null; | ||
+ | Enumeration e = FileSystemRegistry.listRoots(); | ||
+ | while (e.hasMoreElements()) | ||
+ | { | ||
+ | root = (String)e.nextElement(); | ||
+ | if(root.equalsIgnoreCase("sdcard/")) | ||
+ | { | ||
+ | sdCardPresent = true; | ||
+ | } | ||
+ | } | ||
+ | if(!sdCardPresent) | ||
+ | { | ||
+ | UiApplication.getUiApplication().invokeLater(new Runnable() | ||
+ | { | ||
+ | public void run() | ||
+ | { | ||
+ | Dialog.alert("This application requires an SD card to be present. Exiting application..."); | ||
+ | System.exit(0); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | try{ | ||
+ | // create a URI object with te file name and location | ||
+ | String dbLocation = "/SDCard/databases/DBCreator/"; | ||
+ | URI uri = URI.create(dbLocation + DB_NAME); | ||
+ | |||
+ | // open a database connection to that URI | ||
+ | Database db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false)); | ||
+ | |||
+ | // create the table if it does not already exist | ||
+ | Statement statement = db.createStatement("CREATE TABLE IF NOT EXISTS Student(id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, email TEXT, address TEXT)"); | ||
+ | statement.prepare(); | ||
+ | |||
+ | //execute the statement | ||
+ | statement.execute(); | ||
+ | |||
+ | // close statement and database | ||
+ | statement.close(); | ||
+ | db.close(); | ||
+ | |||
+ | }catch(Exception ex ) { | ||
+ | errorDialog(ex.getMessage()); | ||
+ | System.exit(0); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | // Error message dialog | ||
+ | public static void errorDialog(final String message) | ||
+ | { | ||
+ | UiApplication.getUiApplication().invokeLater(new Runnable() | ||
+ | { | ||
+ | public void run() | ||
+ | { | ||
+ | Dialog.alert(message); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
===11.3 Add id to Student Class=== | ===11.3 Add id to Student Class=== |
Revision as of 21:32, 11 April 2011
Contents
- 1 11. Use Database and SQLite to Store Data
- 1.1 11.1 Add SDCard to Simulator
- 1.2 11.2 Create Database File
- 1.3 11.3 Add id to Student Class
- 1.4 11.4 Check for presence of SDCard
- 1.5 11.5 Copy Database File into SDCard
- 1.6 11.6 Create SQLiteManager Class
- 1.7 11.7 Modify StudentsList Class to use SQLiteManager
- 1.8 11.8 Student View Application in Action
11. Use Database and SQLite to Store Data
11.1 Add SDCard to Simulator
To be able to have data preserved on database, first we need to make sure that simulator can simulate SDCard, so that we can run our application with database. For this purpose first create a folder on your file system. Then run the simulator and from the top menu choose: Simulate > Change SD Card... .
Then Mount Directory, and select the folder you just created. Every time you restart the simulator, before running the app you need to make sure that the folder is mounted as SD Card. later on you will see that the database file gets created there.
11.2 Create Database File
We would like to create a database file including out Student Entity, so that we can put it in the project. Then every time the app runs, first it checks if it is the first time run of the app on the device or simulator. If so, the app will copy the file in to the SDCard. Next time that you run the application, it will know that the file is there, so will not override it.
For this purpose, create a simple blackberry project with only the application object. We do not need any screen here. call it "DBCreator". and this will need to only have a class called MyApp.java which extends UIApplication. Here is the code for this class:
package mypackage;
import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.database.*;
import net.rim.device.api.io.*;
/**
* This class extends the UiApplication class, providing a
* graphical user interface.
*/
public class MyApp extends UiApplication
{
private static String DB_NAME = "SQLiteStudentList";
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args) throws Exception
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()throws Exception
{
// Push a screen onto the UI stack for rendering.
boolean sdCardPresent = false;
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements())
{
root = (String)e.nextElement();
if(root.equalsIgnoreCase("sdcard/"))
{
sdCardPresent = true;
}
}
if(!sdCardPresent)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("This application requires an SD card to be present. Exiting application...");
System.exit(0);
}
});
}
else
{
try{
// create a URI object with te file name and location
String dbLocation = "/SDCard/databases/DBCreator/";
URI uri = URI.create(dbLocation + DB_NAME);
// open a database connection to that URI
Database db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false));
// create the table if it does not already exist
Statement statement = db.createStatement("CREATE TABLE IF NOT EXISTS Student(id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, email TEXT, address TEXT)");
statement.prepare();
//execute the statement
statement.execute();
// close statement and database
statement.close();
db.close();
}catch(Exception ex ) {
errorDialog(ex.getMessage());
System.exit(0);
}
}
}
// Error message dialog
public static void errorDialog(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(message);
}
});
}
}
11.3 Add id to Student Class
11.4 Check for presence of SDCard
11.5 Copy Database File into SDCard
11.6 Create SQLiteManager Class
11.6.1 Open Database
11.6.2 Close Database
11.6.3 Select All Students
11.6.4 Insert New Student
11.6.5 Delete Student
11.6.6 Update Student
11.7 Modify StudentsList Class to use SQLiteManager
11.7.1 Load list
11.7.2 Add Student
11.7.3 Edit Student
11.7.4 Delete Student