Jona
Jona

Reputation: 13555

How does sqlite3 manage SELECT query data?

I'm trying to understand how sqlite3 works when doing a SELECT query. I do see that in order to read the results you have to loop around using sqlite3_step to go to the next row.

Example

sqlite3_prepare_v2(db, "select name;", -1, &stmt, NULL);

sqlite3_bind_int(stmt, 1, 16);                                                                  

while ( (rc = sqlite3_step(stmt)) == SQLITE_ROW) {                                              
    printf("%s\n", sqlite3_column_text(stmt, 0));
}

sqlite3_finalize(stmt);

My question is how is the SELECT data stored? In memory or on file? Also, how can I read a particular row without having to loop around the entire result or having to read the entire SELECT query into memory?

Upvotes: 0

Views: 131

Answers (1)

CL.
CL.

Reputation: 180060

The data is stored in the database file on disk. Queried data is loaded into memory.

Result rows are computed on demand, so the query never needs to be entirely in memory; only the current row is accessible, and the only movement you can do is to try to step to the next row.

If you don't want to step through all result rows, change the query to return only the desired rows.

Upvotes: 1

Related Questions