Disclaimer: I am using sqlite3 version 3.7.17.
I am trying to insert about 45000 rows in an empty sqlite table called block. I don't want to use multiple insert statements, for it takes about 12 second to do all insertions (even when I group everything inside "BEGIN TRANSACTION" / "END TRANSACTION").
I then looked for bulk insertion methods in sqlite, and I found this topic, which apparently helped a lot of people. I tried reproducing both version of the provided pseudo-code (using both UNION ALL and INSERT VALUES). When I print my queries, they all look correct. However, the data is not being written to the database.
This is my code:
const char* mega_query_blocks =
"insert into block "
"select ? as p, ? as q, ? as x, ? as y, ? as z, ? as w "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ? "
"union all select ?, ?, ?, ?, ?, ?";
- (void) _db_mega_insert_block: (Block *) block atPosition: (int) position
{
int mod_position = position % 8;
if (mod_position == 1 && position > 1) {
sqlite3_step(data->mega_blocks_stmt);
sqlite3_clear_bindings(data->mega_blocks_stmt);
sqlite3_reset(data->mega_blocks_stmt);
}
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 1, block.p);
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 2, block.q);
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 3, block.x);
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 4, block.y);
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 5, block.z);
sqlite3_bind_int(data->mega_blocks_stmt, 6 * (mod_position - 1) + 6, block.w);
}
sqlite3_open([filePath UTF8String], &data->db);
sqlite3_prepare_v2(data->db, mega_query_blocks, -1, &data->mega_blocks_stmt, NULL);
sqlite3_exec(data->db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
// iterate over blocks here
[self _db_mega_insert_block:block atPosition:position];
sqlite3_exec(data->db, "END TRANSACTION;", NULL, NULL, NULL);
sqlite3_finalize(data->mega_blocks_stmt);
sqlite3_close(data->db);
Do you have any ideas of why the data is not being inserted, even though all queries are printed correctly, which valid values substituting the question marks?
Thank you!
PS: Obviously the code is not linearly shown here, but in practice that is what it is happening.