Saturday, 22 April 2017

sqlite - Android bulk insert syntax error near ","

I would appreciate a shade of enlightenment on the following code, since I just can't seem to figure out why it has a syntax error near ",".




I'm trying to insert multiple rows at once in a single statement. I'm targeting API 10+. If I only have one set of VALUES(...) it's all good, and the row gets inserted, but as soon as I have more than one, it sends out an exception with the syntax error message.



SQLiteDatabase db = getWritableDatabase();
String insertRows = "INSERT INTO " + TABLE_FRIENDS +
" (" + KEY_USER_EMAIL + ", " + KEY_FRIEND + ", " + KEY_FRIEND_INTERACTIONS +
", " + KEY_ENABLED +") VALUES ";

int index;
for(index=0; index insertRows += "('" + userAccount.getEmail() +"', ";


Friend friend = friends.get(index);

insertRows+= "'" + friend.getEmail() +"', ";
insertRows+= "'" + friend.getInteractions() +"', ";

int enabled = friend.isEnabled() ? 1 : 0;

insertRows+= "'"+ enabled + "')";


if(index != friends.size()-1)
insertRows+= ",";
}

insertRows += ";";

db.execSQL(insertRows);
db.close();



I print the query out on my log and get:



INSERT INTO friends (user_email, friend_email, friend_interactions, friend_enabled) VALUES ('test2@test.com', 'vila@hotmail.com', '0', '0'),
('test2@test.com', 'anotherone@hotmail.com', '0', '0'),
('test2@test.com', 'yetanother@hotmail.com', '0', '0');


Help is very much appreciated.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...