SQLite Forum

The second query return nothing
Login

The second query return nothing

(1) By Forrest Ling (forrestling) on 2021-05-01 09:53:06 [link] [source]

I can run sqlite3_exec () successfully for the first time, then continue run sqlite3_exec() returns nothing.

static int callback(void *ret, int argc, char **argv, char **azColName) { int i; static int len = 0; for(i=0; i<argc; i++){ len += snprintf ( ret + len, CONTENT_SIZE - len ," %s ",argv[i] ? argv[i] : "NULL" ); } len += snprintf ( ret + len, CONTENT_SIZE - len ,"
" ); return 0; }

main() { char ret[8096]; rc = sqlite3_exec(db, sql, callback, ret, &zErrMsg);

}

Can you please advise some suggestions ?

Thanks, Forrest

(2) By Larry Brasfield (larrybr) on 2021-05-01 10:21:39 in reply to 1 [source]

I can run sqlite3_exec () successfully for the first time, then continue run sqlite3_exec() returns nothing.

The code you have shown calls sqlite3_exec() exactly once. That seems inconsistent with "continue run sqlite3_exec()". I guess you mean either: "My additional calls, which are to be assumed are just like the one shown, return nothing." ; or "I expected the callback to be run multiple times when sqlite3_exec() is called once." I suggest you clarify before expecting answers. I can guess what you are asking, then answer each of the guessed questions, but that is too inefficient for my taste.

I also see that you are adding an integer to a void *. There is a compiler which allows that, but most do not. I suggest you stop doing that before it becomes a habit.

(3) By Forrest Ling (forrestling) on 2021-05-02 03:49:44 in reply to 1 [link] [source]

I solved the problem. I found "static int len" the static variable in callback is thread unsafe, the variable can be modified by other thread. I added one line to calculate the len of char ret, instead of static variable. the problem solved.

but it increase some workload of strlen for the char ret.

(4) By Forrest Ling (forrestling) on 2021-05-02 04:02:27 in reply to 2 [link] [source]

Thanks Larry for quick response. Sorry I cannot state my problem clearly. The problem is when I rerun the sqlite3_exec() , by the second hitting the url to query the database, (such as select * from users; ) , it returns nothing. the first run of Sqlite3_exec() is correct by returning a list of users rows, As I use static variable in callback to remember the position of char * ret. I google searched static variable is thread unsafe, therefore I calculate string length in the callback, it seems the problem was solved.
Thanks have a good weekend.