3

I have a database which already contains a table "LocalLogin" and is properly set and can be queried without a problem, being set up the same way as my new table "PersonList". However when I try to execute a query on PersonList to select some values, I get the error

android.database.sqlite.SQLiteException: no such table: PersonList: , while compiling: SELECT ...

It gives me the idea that the table was never created, although I execute a create query in the onCreate method of my SQLiteOpenHelper class. Is having the same Database Name as LocalLogin the possible problem?

Here's the relevant code:

The 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);

2 Answers 2

5

If you're adding a new table, you need to increment your database version so onUpdate will fire and your database will be recreated with the new table.

Sign up to request clarification or add additional context in comments.

2 Comments

I added " @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(DATABASE_CREATE); }" and changed the database version to 2 but I still have the same error
Works like a charm, I incremented the database version in both database adapters, which I apparently shouldn't have done
0

You declare 'private static final String DATABASE_CREATE' twice. I'm surprised this compiles, actually? That said, you should add a logging statement to the db.execSQL(DATABASE_CREATE); call, to see if it actually succeeds.

2 Comments

I have edited my code. I left big irrelevant parts out to don't post too much here and went wrong with a copypaste
Fair enough; however you still need to ascertain whether the table is being created. Your syntax looks right to me, but it could be quietly failing due to some other reason, meaning the table isn't there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.