Search code examples
androidandroid-activityandroid-sqliteandroid-adaptersimplecursoradapter

Getting android Sqlite syntax error running in api 14


I have sequence of database adapter, which dumps default values in to the tables. It works fine on the devices running on API level 19 to 21. When I tried it in API 14. I'm getting the syntax error.

I/SqliteDatabaseCpp﹕ sqlite returned: error code = 1, msg = near ",": syntax error, db=/data/data/com.myapp/databases/myappdb

Here are my sql lines

private static final String DATABASE_INSERT_SEARCH =
"INSERT INTO varngle_word (name, count, type) VALUES" +
                    " ('Kindle', 0, 'item')," +
                    " ('Google', 0, 'site');";

private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE_SEARCH);
            db.execSQL(DATABASE_INSERT_SEARCH);
   }
}

Solution

  • As per this post multi-values syntax was introduced in SQLite version 3.7.11: "Enhance the INSERT syntax to allow multiple rows to be inserted via the VALUES clause. ".

    SQLite version 3.7.11 is only in Android 4.1 and up.

    • SQLite version 3.7.11 available with API levels: 19, 18, 17, 16
    • SQLite version 3.7.4 available with API levels: 15, 14, 13, 12, 11

    Check this post for more details about SQLite versions.

    Alternatively you could loop through items and insert row by row or use the following:

     INSERT INTO varngle_word (name, count, type) 
     SELECT 'Kindle', 0, 'item'
      UNION
     SELECT 'Google', 0, 'site';
    

    See this Fiddle.