Assuming you have a table with the following schema: CREATE TABLE t1 ( id INTEGER PRIMARY KEY AUTOINCREMENT, col2 REAL, col3 TEXT );
your C code *could* look something like the following: sqlite3_exec(db, "INSERT INTO t1 (col2,col3) VALUES (1.0,'row one');", 0, 0, 0); rowid = sqlite3_last_insert_rowid(db); HTH. -Shane On Mon, Feb 8, 2010 at 2:59 PM, Vasanta <vtan...@gmail.com> wrote: > I understand "SELECT last_insert_rowid()" gives rowird, but I have to call > these SQL statements in C language code, for that I have to sqlite3_prepare > and sqlite3_step() calls, I am looking for sample of sqlite3_step, how that > return the rowid, can I call like this: > > int rowid; > sqlite3_stmt *pStmt; > > sqlite3(pDB, "SELECT last_insert_rowid()", -1, &pStmt, 0); > rowid = sqlite3_step(pStmt); > > On Mon, Feb 8, 2010 at 2:36 PM, Kees Nuyt <k.n...@zonnet.nl> wrote: > > > On Mon, 8 Feb 2010 11:44:39 -0500, Vasanta > > <vtan...@gmail.com> wrote: > > > > > I tried to use this function call > > > "sqlite3_last_insert_rowid()< > > http://www.sqlite.org/c3ref/last_insert_rowid.html>" > > > calling from C language function, but it always returns zero, any > idea?. > > > I have valid DB handle. > > > > The function only returns the rowid of the last successful > > INSERT statement on the same connection / DB handle. It > > tells you which row has been inserted. > > > > Perhaps you expected it to predict which row would be > > inserted on the next INSERT statement? > > > > If you need a new ID for every row you insert, don't try to > > find out which value to use, but let SQLite do the work. > > > > Sample code: > > > > CREATE TABLE t1 ( > > id INTEGER PRIMARY KEY AUTOINCREMENT, > > col2 REAL, > > col3 TEXT > > ); > > > > BEGIN; > > INSERT INTO t1 (col2,col3) > > VALUES (julianday('now'),'row one'); > > SELECT last_insert_rowid(); > > INSERT INTO t1 (col2,col3) > > VALUES (julianday('now'),'row two'); > > SELECT last_insert_rowid(); > > INSERT INTO t1 (col2,col3) > > VALUES (julianday('now'),'row three'); > > SELECT last_insert_rowid(); > > COMMIT; > > > > Read http://www.sqlite.org/c3ref/last_insert_rowid.html > > again for more details. > > -- > > ( Kees Nuyt > > ) > > c[_] > > _______________________________________________ > > 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 > _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users