I am creating an android app, and using a database with it. One of the tables I am using with it is a semester table, which is created using the following statement:
final String createSemesterTable =
"CREATE TABLE IF NOT EXISTS" + semesterTable +
"( " + _id + " INTEGER PRIMARY KEY AUTOINCREMENT," +
semesterName + " TEXT," +
isCurrent + " INTEGER," +
GPA + " REAL" + ");";
simple version:
CREATE TABLE IF NOT EXISTS Semesters(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_name TEXT,
is_current TEXT
GPA REAL);
In my main activity, I try to create a Cursor Adapter that links to a list view, using the data from the database to fill the items.
Here is part of my onCreate(..) method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
dbase = new DBHelper(getBaseContext());
sqldbase = dbase.getWritableDatabase();
ListView list = (ListView)findViewById(R.id.semesterList);
String columns[] = {DBHelper._id, DBHelper.semesterName, DBHelper.isCurrent, DBHelper.GPA};
String from[] = {DBHelper.semesterName, DBHelper.GPA};
int to[] = {R.id.semesterName, R.id.semesterGPA};
Cursor cursor = sqldbase.query(DBHelper.semesterTable, columns, null, null, null, null, null);
adap = new SimpleCursorAdapter(getBaseContext(),R.layout.semester_list_view,cursor, columns, to);
list.setAdapter(adap);
}
When I compile/run, I get the following message, however:
07-06 20:34:25.950: E/AndroidRuntime(2825): java.lang.RuntimeException: Unable to start activity
android.database.sqlite.SQLiteException: no such column: _id (code 1): ,
while compiling: SELECT _id, Semester_Name, Is_Current, GPA FROM Semesters
I don't see what is wrong, because I am using a _id column (which is actually required by SQLite). Also, why is _id column required (by that name)? If I have multiple tables, would I be able to set different names for their primary keys, or will _id be required for each?
Thanks.
"... EXISTS " + semesterTable ...