I found this excellent example of a SQLiteOpenHelper class which is really nicely documented, and I think I can use it for my application; the thing is I'm still struggling to determine if the database created by my app already exists
My app has a main activity Java class [imaginatively named] 'ActivityMain
' which at the moment calls the void dbTest();
public void DBTest() {
SQLiteDatabase myDB = null;
/* Create a Database. */
try {
myDB = this.openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE
+ " (" + KEY_ID + " integer primary key autoincrement, "
+ KEY_SCRIPT_NAME + " text, " + KEY_SCRIPT_BODY + " text, "
+ KEY_SU_NEEDED + " short);");
/* Insert data to a Table*/
myDB.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (" + KEY_SCRIPT_NAME
+ ", " + KEY_SCRIPT_BODY
+ ", " + KEY_SU_NEEDED + ")"
+ " VALUES ('CPU information', 'cat /proc/cpuinfo', 1);");
/*retrieve data from database */
Cursor c = myDB.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
int scriptName = c.getColumnIndex("name");
// Check if our result was valid.
c.moveToFirst();
// cursor left as it came from the database because it starts at the row before the first row
ArrayList<String> sData = new ArrayList<String>();
if (c != null) {
do {
String Name = c.getString(scriptName);
sData.add(Name);
} while (c.moveToNext());
}
ListView lv = (ListView) findViewById(R.id.mainListView);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, sData));
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} catch (Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
In there it creates the database an inserts a line then displays it back in a ListView, currently this will just insert the same row each time the app is started so I want to check to see if the database is already there.
I want the have the db start out with some initial values on there thats why I'm inserting the data, the user can then delete it if they like
So in my 'ActivityMain
' class I want to call the createDataBase();
method from the 'DatabaseHelper
' class I have used from the sample linked but can't
Is it something like:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
try {
createDataBase();
catch(SQLiteException e){
// database doesn't exist yet.
return false;
}
I have a boolean function in the 'ActivityMain
' class which works fine but I'm not sure that this is the right way to do it and in fact should be doing all of this through the helper class
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
return false;
}
return checkDB != null ? true : false;
}
Could someone give me some pointers please?
Many thanks :)