CQM CQM - 27 days ago 11
SQL Question

Android SQLite database not populating

I am trying to create an SQLite database. The create method:

private static class DatabaseHelper extends SQLiteOpenHelper {

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_BASE_TABLE);
}

}


(the constructor and the upgrade function are implemented in that private class as well)

is calling the SQL string:

static final String CREATE_BASE_TABLE ="create table if not exists tableData ("
+ "id INTEGER PRIMARY KEY,"
+ "name TEXT,"
+ "image TEXT,"
+ "var1 TEXT,"
+ "var2 TEXT,"
+ ");";


but the
execSQL
returns
success=false
, as revealed by the debugger and breakpoints.

In File Explorer I see that
data
is created in the databases folder. I open that file on my local machine and only see a table called
android_metadata
not
tableData
and my fields are nowhere to be seen!

How do I fix it to make my method properly populate my table? Is there a syntax issue with my query?

Answer

It's very subtle but the error is in your create table command. This line should not have a comma:

+ "var2 TEXT,"

Instead, it should just be:

+ "var2 TEXT"

since it is the last column you are defining.