Reputation: 29
After some study, I saw that SQLite will handle concurrent read . But after I try to create multi-thread and read the SQLite simultaneously, it will crash on sqlite3_get_table. But if I use serialized mode, it will be totally fine. Why this happened? Did I misunderstand something?
And here is how I read the data:
ret = sqlite3_get_table(db, sql, &results, &rows, &columns, &err_msg);
if (ret != SQLITE_OK) {
// error handling
}
if (rows < 1) {
// error handling
}
else {
// reading data
}
sqlite3_free_table(results);
I could also add lock/unlock around sqlite3_get_table to solve the problem. But why I can't just call this function without locking?
Upvotes: 2
Views: 2317
Reputation: 180070
The linked answer talks about concurrent accesses from multiple processes.
Concurrent accesses are perfectly fine as long as each access goes through its own connection. (Reads are allowed, writes are properly locked.)
Your program should use one connection per thread.
Upvotes: 1