Android "android.database.sqlite.SQLiteException: No such table" when executing a query on the database
I have a database that already contains the "LocalLogin" table and is configured correctly and can be queried without issue, configured the same as my new "PersonList" table. However, when I try to query the PersonList to select some values, I get the error
android.database.sqlite.SQLiteException: no such table: PersonList: , while compiling: SELECT ...
It seems to me that the table was never created even though I am executing a create query in the onCreate method of my SQLiteOpenHelper class. Has the same database name as LocalLogin a possible problem?
Here's the relevant code:
Database adapter class
public class GoingOutPersonListDbAdapter {
private static final String DATABASE_NAME = "GoingOutData";
private static final String DATABASE_TABLE_PERSONLIST = "PersonList";
private static final int DATABASE_VERSION = 1;
public static final String PERSONLIST_ID = "PersonList_id";
public static final String PERSONLIST_LOGIN = "Login";
public static final String PERSONLIST_PASSWORD = "Password";
private static final String TAG = "Debugstring";
private PersonListDatabaseHelper mPersonListDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"CREATE Table " + DATABASE_TABLE_PERSONLIST+ " ( "
+ PERSONLIST_ID + " integer PRIMARY KEY Autoincrement, "
+ PERSONLIST_LOGIN + " text NOT NULL,"
+ PERSONLIST_PASSWORD + " text NOT NULL );";
private final Context mCtx;
private static class PersonListDatabaseHelper extends SQLiteOpenHelper {
PersonListDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG,DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
}
public GoingOutPersonListDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public GoingOutPersonListDbAdapter open() throws SQLException {
mPersonListDbHelper = new PersonListDatabaseHelper(mCtx);
mDb = mPersonListDbHelper.getWritableDatabase();
return this;
}
public Cursor searchPerson(String searchString) {
return mDb.query(DATABASE_TABLE_PERSONLIST, new String[] {PERSONLIST_ID, PERSONLIST_LOGIN, PERSONLIST_PASSWORD}, searchString, null, null, null, null);
}
}
In my activity class:
private GoingOutPersonListDbAdapter mPersonListDbHelper;
...
mPersonListDbHelper = new GoingOutPersonListDbAdapter(this);
mPersonListDbHelper.open();
...
//loginEditText is properly set
Cursor personList = mPersonListDbHelper.searchPerson(GoingOutPersonListDbAdapter.PERSONLIST_LOGIN + " = '" + loginEditText + "'");
startManagingCursor(personList);
If you add a new table, you need to increase your database version to be included in onUpdate and your database will be recreated with the new table.
You are declaring 'private static final String DATABASE_CREATE' twice. I'm surprised this compiled? However, you must add a logging statement to db.execSQL (DATABASE_CREATE); a call to check if this is actually happening.