Following on from my previous question about tables I am trying to create additional tables on my database in Android.
Basically, I would like a database to be created which is done so by this code:
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
Then when a user adds an account I would like a table added with the name of the account the user entered. I try to call the createTable() method I created.
public void createAccount(String account) {
DATABASE_TABLE = account;
db.execSQL(TABLE_CREATE);
System.out.println(DATABASE_NAME);
}
But the db doesn’t work as eclipse cannot find the variable. I try mDb which is my SQLiteDatabase but I get a null pointer exception.
*Edit*
I really can’t get my head around the database. I am using the For Dummies book and a Database helper but from where I am at now I feel a lot of the code is done behind the scenes and it is hard to follow.
This is part of my database class:
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
System.out.println(DATABASE_TABLE);
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void createAccount(String account) {
DATABASE_TABLE = account;
/* SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("create table long ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_DESCRIPTION + " text not null, "
+ KEY_DEBIT + " text not null);");
}
}
At the bottom of the class I have
public void changeDatabase(String account) {
DATABASE_TABLE = account;
mDbHelper.createAccount(account);
}
which is called upon from my createAccount class.
I am getting a null point exception.
This is without seeing your code…
I’m guessing you’ve inherited / extended from one of the SQLite Classes.
you see how you’ve implemented onCreate() and are provided the database as a parameter,
if you create a new function, like that createAccount function you need to get a handle of the db as follows:-