0

I am trying to insert two rows of values to just two columns on a single query, following this much-decorated question, Inserting multiple rows in a single SQL query?, but with my code

DatabaseHelper helper = new DatabaseHelper(RegisterActivity.this);
database = helper.getWritableDatabase();
database.query("usersdata", fields, null, null, null, null, null);
database.rawQuery("INSERT INTO usersdata ( userName, userType ) VALUES " + "( '" +   firstTime + "' , 'flag' ) , ( '" + name + "' , 'Owner' )", null);
database.close();

I receive the error

android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: INSERT INTO usersdata ( userName, userType ) VALUES ( 'hasBeenRegistered' , 'flag' ) , ( 'kwish' , 'Owner' )

It seems that my quotes aren't an issue, as it says: near "," which "," it has a problem with I don't know...I've also tried with no space after the quotes--not that that should matter. Any help on this would be greatly appreciated! TIA (Now seeing I didn't need to concatenate VALUES and the (, but that also shouldn't matter!)

Community
  • 1
  • 1
kwishnu
  • 1,730
  • 19
  • 17
  • possible duplicate of [Is it possible to insert multiple rows at a time in an SQLite database?](http://stackoverflow.com/questions/1609637/is-it-possible-to-insert-multiple-rows-at-a-time-in-an-sqlite-database) – Bart Friederichs Apr 18 '13 at 19:25
  • http://stackoverflow.com/questions/1609637/is-it-possible-to-insert-multiple-rows-at-a-time-in-an-sqlite-database – stinepike Apr 18 '13 at 19:30

2 Answers2

1

According to the documentation, Android ships with SQLite 3.4.0. And what you want to do, is not supported until SQLite 3.7.11.

You have to rewrite like this (from this answer):

INSERT INTO 'tablename'
      SELECT 'data1' AS 'column1', 'data2' AS 'column2'
UNION SELECT 'data3', 'data4'
UNION SELECT 'data5', 'data6'
UNION SELECT 'data7', 'data8'
Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Thanks, it seems this is putting me on the right track. One additional stumbling block, my auto-incremented _id column is begging for a value with the query stated as above...nvm, I thought I should try 'null' as the value before shooting this out there, and that was the ticket. Thanks very much! – kwishnu Apr 18 '13 at 20:30
0

just a guess here, but the post you reference is specifically about SQLServer. it's quite possible that SQLite doesn't support multirow inserts in a single query - or maybe not in the same syntax. take a look here - Is it possible to insert multiple rows at a time in an SQLite database?

Community
  • 1
  • 1
Chris Piazza
  • 251
  • 2
  • 6