Reputation: 316
I'm working on my first Android project (full disclosure, this is a school project). I must confess I'm finding it to be much more convoluted than I expected. Anyway, here's my issue.
I've created a DBHelper class:
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "asteroids.sqlite";
private static final int DB_VERSION = 1;
public DBHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
System.out.println("DB HELPER ON CREATE FUNCTION HERE!!!!");
db.beginTransaction();
resetAsteroidsTable(db);
// Call functions to create each table.
// Create/Reset BGObject Table
resetBGObjectsTable(db);
// GameLevels
resetGameLevelsTable(db);
// Level Objects
resetLevelObjectsTable(db);
// Level Asteroids
resetLevelAsteroidsTable(db);
// Main Bodies
resetMainBodiesTable(db);
// Cannons
resetCannonsTable(db);
// Extra Parts
resetExtraPartsTable(db);
// Engines
resetEnginesTable(db);
// Power Cores
resetPowerCoresTable(db);
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
return;
}
The functions getting called to create individual tables are of this format:
public void resetAsteroidsTable(SQLiteDatabase db)
{
// Create/Reset Asteroids Table
final String SQL =
"DROP TABLE IF EXISTS ASTEROIDS; " +
"CREATE TABLE ASTEROIDS " +
"(" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" IMAGEPATH TEXT NOT NULL, " +
" IMAGEWIDTH INTEGER NOT NULL, " +
" IMAGEHEIGHT INTEGER NOT NULL, " +
" ASTYPE TEXT NOT NULL " +
");";
System.out.println("Create the ASTEROIDS database!");
System.out.println(SQL);
db.execSQL(SQL);
I've checked the create table SQL statements manually and they do work.
However, when I call this function also found in DBHelper to insert data into one of the tables.
public boolean executeSqlStatement(String SQLStatement)
{
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.execSQL(SQLStatement);
db.setTransactionSuccessful();
db.endTransaction();
return true;
}
I get the error:
android.database.sqlite.SQLiteException: no such table: ASTEROIDS (code 1):
I'm confused because I know that onCreate is getting called because I see my print statements in logcat. I'm clearing the app data each time I run my test that is doing this operation, and I do have access to the database dump that I'm getting using the humpty script. The database schema shows only a table called android_metadata which is empty.I've been looking online for hours, I'd really appreciate any pointers here.
Upvotes: 2
Views: 803
Reputation: 152847
exeqSQL()
executes just one SQL statement. You need to split your SQL to individual calls, one for DROP TABLE
and another for CREATE TABLE
. Note that onCreate()
is only invoked when the database file didn't exist so the DROP TABLE
is really not needed.
Another issue is that SQLiteOpenHelper
lifecycle methods such as onCreate()
are invoked inside an transaction. You should not start a nested transaction yourself. SQLite itself does not support nested transactions, and Android's SQLite wrapper only adds partial support for transaction nesting.
Upvotes: 4