Monday, February 18, 2019
sqlite - Android bulk insert syntax error near ","
Answer
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.
Subscribe to:
Post Comments (Atom)
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...
-
This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm...
-
I need to do the following: My current address looks like: https://www.domain.com I want to redirect with htaccess: www.domain.com TO https:...
-
using namespace std; So far in my computer science courses, this is all we have been told to do. Not only that, but it's all tha...
No comments:
Post a Comment