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 ,ContentValue
s , 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
?
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With