Thursday, March 29, 2018

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...