0

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.

2
  • How about adding a space after "EXISTS"? Like "... EXISTS " + semesterTable ... Commented Jul 6, 2013 at 21:12
  • ya i figured that out ho.. Commented Jul 6, 2013 at 21:30

1 Answer 1

1

According to Logcat, I think you have to delete your database (may be by uninstalling your app) and then try running the app. This will recreate your database. One possibility is that you have the table already created and a select somewhere is failing because the _id column has never been created.

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

1 Comment

Hell yes! that worked! I think I might add DROP TABLE statements in my onDestroy() methods for now. thanks.

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.