SQLite syntax error near "CREATE TABLE"

12,182

The CREATE TABLE syntax is all right as you've posted it.

I suspect there's a non-breaking space (ASCII 0xA0) between CREATE and TABLE. Replace it with the regular space (ASCII 0x20). That would explain the syntax error you posted: parser is treating CREATE TABLE as a single unknown token and not as two separate known tokens CREATE and TABLE.

What is definitely wrong is that you call db.close() on the SQLiteDatabase db passed in as a parameter to your function. You should only close databases you opened yourself and closing it this way will lead to an exception, albeit a different one.

Share:
12,182
nath_vringd
Author by

nath_vringd

Updated on July 16, 2022

Comments

  • nath_vringd
    nath_vringd almost 2 years

    Here's my code:

        String CREATE_DATABASE = "CREATE TABLE " + TABLE_NAME + "("  + 
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                "title TEXT, "+
                "author TEXT, "+
                "state TEXT);";
    
        db.execSQL(CREATE_DATABASE);
    

    The LogCat says : 12-11 23:43:50.553: E/AndroidRuntime(3706): android.database.sqlite.SQLiteException: near "CREATE TABLE": syntax error (code 1): , while compiling: CREATE TABLE PapersTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, state TEXT);