0

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);
   }
}
3
  • DATABASE_INSERT_SEARCH seems to work (sqlfiddle.com/#!5/1f2be) where is your DATABASE_CREATE_SEARCH? Commented Mar 13, 2015 at 19:14
  • @Trinimon DATABASE_CREATE_SEARCH is another string to create table. no problem in that. "create table varngle_word (_id integer primary key autoincrement, " + "name text not null, " + "count int not null, " + "type text not null);"; Commented Mar 13, 2015 at 19:24
  • I know, but I wondered whether table creation could have an impact on the insert. Meanwhile I found it - see answer below ... Commented Mar 13, 2015 at 19:28

2 Answers 2

1

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.

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

2 Comments

Does it supports all version of sqlite?
INSERT INTO (...) SELECT ... is pretty much SQL standard. There is a note on 3.5.3: "Fix a long-standing bug in INSERT INTO ..." but this is not applicable to any SQLite versions used with Android. Take care with 3.8.5: "Fix a bug ... that caused crashes when doing an INSERT INTO ... SELECT statement ...".
1

You can't use this syntax to insert multiple rows. It comes after sqlite version 3.7.11, and android 4.0 devices usually comes with sqlite 3.7.4.

see this link (Insert Error SqLite in Android 4.0.3) for some solutions

Comments

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.