SQLite Forum

Can I read(select) parallelly on the same connection in multi-threads?
Login

Can I read(select) parallelly on the same connection in multi-threads?

(1.1) By ss53ee on 2021-05-22 14:25:38 edited from 1.0 [link] [source]

libsqlite3.lib complied by SQLITE_THREADSAFE = 1, it's run on serialize mode.

I use a single connection(sqlite3_open("xx.db", &db)),and pass the sqlite3* pointer to two other threads.and the two threads execute code below.

const char *sql = "SELECT name, age FROM t_person WHERE age < 30;";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(sql, sql, -1, &stmt, NULL) == SQLITE_OK)
{
    while(sqlite3_step(stmt) == SQLITE_ROW) 
    {
        const unsigned char *name = sqlite3_column_text(stmt, 0);
        int age = sqlite3_column_int(stmt, 1);
	printf("%d", age);
    }
}

i have two question.
1.is that code thread safe?
2.when use code below replace the before code.
"BEGIN TRANSACTION"
select * from t_person;
select age from t_person;
"COMMIT"
this transaction is as read or write? is it threadsafe?

(2) By Keith Medcalf (kmedcalf) on 2021-05-22 17:01:23 in reply to 1.1 [source]

Insufficient information to provide any conclusion since it is impossible to determine the scope of the variables sql and stmt (which has nothing whatsoever to do with the sqlite3 library).

Secondly, if SQLITE_THREADSAFE is 1 (the default) then the SQLite3 library will ensure entrance requirements thus removing SQLite3 from the equation since even if you write shitty code, your deficiencies will not be permitted to impact the correct operation of the library -- though it will not prevent you from buggering yourself.

(3) By ss53ee on 2021-05-22 23:34:19 in reply to 2 [link] [source]

thank you.