Re: [sqlite] insert statement using temp variable

2011-04-05 Thread Igor Tandetnik
RAKESH HEMRAJANI  wrote:
> int i=0;
> rc = sqlite3_exec(db, "insert into emp values(i);", 0, 0, );

Use sqlite3_prepare_v2, sqlite3_step, sqlite3_bind_* et al to run a 
parameterized query. Something like this:

sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, "insert into emp values(?);", -1, , NULL);
for (int i = 0; i < 10; ++i) {
  sqlite3_bind_int(stmt, 1, i);
  sqlite3_step(stmt);
  sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);

This example also shows how you can run the same statement multiple times with 
different values for parameters.
-- 
Igor Tandetnik

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] insert statement using temp variable

2011-04-04 Thread venkat easwar
Hi,

Very simple, What will be the output of printf("i"); it won't be 0 right?

use snprintf or sprintf and formulate the string then execute the query.

int i=0;

char * a[100];
snprintf(a,100,"insert into emp values(%d);",i); /or
/*sprintf(a,"insert into emp values(%d);",i);*/

rc = sqlite3_exec(db, "create table emp (empid num);", callback, 0, );
rc = sqlite3_exec(db, a, 0, 0, );

This should work.

 VENKAT
Bug the Bugs





From: RAKESH HEMRAJANI 
To: sqlite-users@sqlite.org
Sent: Tue, April 5, 2011 10:51:09 AM
Subject: [sqlite] insert statement using temp variable


hi,

need help with very basic question.. More of C than SQLite.

have a very simple C program using sqlite DB.

..
int i=0;

rc = sqlite3_exec(db, "create table emp (empid num);", callback, 0, );
rc = sqlite3_exec(db, "insert into emp values(i);", 0, 0, );

---

the insert query fails with the message stating no such column i.

the aim is very simple to insert the value of i into empid column but not sure 
how to achieve it.

pls note that value of i is dynamic and wont be hardcoded.

  
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users



  
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users