I have to insert a string into a sqlite data base my command ..
Err=sqlite_exec(DB, "create table tbl5(TEXT varchar(100));", xCallback, (void*)"First Test", &ErrMsg);
Err=sqlite_exec(DB, "insert into tbl5 values ('some string');", xCallback, (void*)"First Test", &ErrMsg);
works fine but when I want to put s="some string"
ie
Err=sqlite_exec(DB, "insert into tbl5 values (s);", xCallback, (void*)"First Test", &ErrMsg);
then this is not working so how to add variable then It is not working so how to insert variable in sqlite database thank u
Don't use
sprintf()
butsqlite3_mprintf()
. Here is the documentation.Otherwise you have a risk of SQL injection.
The resulting query string should be freed using
sqlite3_free()
.Also note the
'%q'
instead of the usual'%s'
.Other than the suggestions already given, you can also use prepared statements with bound parameters (this is also useful if you intend to repeat the statement several times with different parameters). see the
sqlite3_prepare_v2
andsqlite3_bind_*
for more informationYou could use
sprintf
to create a formatted string.It's up to you to make sure
query
is big enough.