6

I am Inserting single row into database using following method for android project.

myDB.execSQL("INSERT INTO "
 + Buss
 + " (BussName, RouteName)"
 + " VALUES ('buss1', 'buss2');");

It work fine. And I see this link Inserting multiple rows in sqlite database. and I try this method(insert multiple rows) in my android project, but It does not work.

myDB.execSQL("INSERT INTO "
 + Buss
 + " (BussName, RouteName)"
 + " VALUES ('buss1', 'buss2'),('buss1', 'buss2'),('buss1', 'buss2');");

How to do it?

3
  • can you show how you create Buss table.. Commented Mar 15, 2014 at 18:05
  • @RanjitPati myDB.execSQL("CREATE TABLE IF NOT EXISTS " + Buss + " (BussName VARCHAR(30),RouteName VARCHAR(30));"); Commented Mar 15, 2014 at 18:10
  • @unluddite you mean BussName, RouteName to 'BussName', 'RouteName' ? I try it it does not work. I can't find any other differents.. Please help if you can find it.. Commented Mar 15, 2014 at 18:16

2 Answers 2

12

You need to call separate insert statement for each row.

For performance reason you can group every few calls (let say ~20) into one transaction:

myDb.beginTransaction();
   for(<your loop definition>){ myDb.execSQL(<your insert statement>) }
myDb.setTransactionSuccessful();
myDb.endTransaction();

The main idea is to not write physical database file on every inserted row, but every few rows. On other had as long as inserted data is not persisted on "drive" it's in the memory. For small data sets you can just start transaction, make all inserts and end transaction in one block.

For bigger data you should make your transactions smaller.

Using prepared statement instead of standard statement is also a good idea, as the SQL interpreter needs to parse query only once - more information can be found here: How do I use prepared statements in SQlite in Android?

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

4 Comments

Is there any alternative methods using myDB.execSQL?
u can use myDb.insert method
@piotrpo Your 1st answer is work. how to use your 2nd answer?
Note: You need to call myDb.setTransactionSuccessful(); before endTransaction() in order for the changes to be committed.
5

Multiple rows insert will work only in SQLite 3.7.11 or above, but only jellybean and kitkat supports Sqlite 3.7.11

4 Comments

which version you are testing? yes i m pretty sure.
you are testing on emulator? check which version on android its using, and if you testing on device, go to setting and see the android version
to find out the sqlite version you can use adb shell sqlite3 --version
4.0.3 android version

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.