Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple insert statements android sqlite

I have 800 insert statements downloaded from network in the following format:

insert into mytable(col1,..,coln) values(val1,..valn);insert into mytable col...

I have heard of transactions ,ContentValues , and using union but I can't figure out which one is better for this case.

What is the best way to insert them in sqlite database after truncating mytable?

like image 990
danrah Avatar asked Nov 16 '13 11:11

danrah


People also ask

How to bulk insert in SQLite?

SQLite doesn't have any special way to bulk insert data. To get optimal performance when inserting or updating data, ensure that you do the following: Use a transaction. Reuse the same parameterized command.

Does SQLite have VARCHAR?

You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact. Your content is never truncated. SQLite understands the column type of "VARCHAR(N)" to be the same as "TEXT", regardless of the value of N.


1 Answers

In case of multiple queries to run, in order to improve data integrity and performance, you should use transactions. This is a code to give you an idea how to do it:

    SQLiteDatabase db = sqlHelper.getWritableDatabase(); // get a writable database here
    db.beginTransaction();

    try {
        for (int insertQuery : listOfQueries) {  // loop through your records 
            db.insert(...);
        }

        db.setTransactionSuccessful();
    }
    finally {
        db.endTransaction();
    }

    db.close();
like image 92
Szymon Avatar answered Oct 06 '22 00:10

Szymon