I think the quick answer is to use a different database handle for your update. 
 I think you may also be able to do this with WAL mode.
http://www.sqlite.org/draft/wal.html
I did some searching and couldn't find a definitive answer for doing an update 
inside a select loop (though I'm sure I've seen it on this list before).
 
I was unable to duplicate your problem...perhaps you can modify this to show it
 
#include <stdio.h>
#include "sqlite3.h"
main()
{
 sqlite3 *db;
 sqlite3_stmt *stmt;
 char *errmsg=NULL;
 char *sql;
 remove("update.db");
 sqlite3_open("update.db",&db);
 sqlite3_exec(db,"CREATE TABLE t (a int, b int)",NULL,NULL,&errmsg);
 sqlite3_exec(db,"insert into t values(1,1)",NULL,NULL,&errmsg);
 sqlite3_exec(db,"insert into t values(1,2)",NULL,NULL,&errmsg);
 sql = "SELECT * FROM t where a=1";
 sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
 while(sqlite3_step(stmt)==SQLITE_ROW) {
  char sqlbuf[4096];
  int ref;
  ref = sqlite3_column_int(stmt,1);
  printf("Before %d\n",ref);
  sprintf(sqlbuf,"UPDATE t set b=b+1 where a=%d",ref);
  puts(sqlbuf);
 
  sqlite3_exec(db,sqlbuf,NULL,NULL,&errmsg);
 }
 sqlite3_finalize(stmt);
 sqlite3_close(db);
 
}
 
Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Northrop Grumman Information Systems
 

________________________________

From: sqlite-users-boun...@sqlite.org on behalf of cricketfan
Sent: Sat 11/6/2010 11:29 PM
To: sqlite-users@sqlite.org
Subject: EXTERNAL:[sqlite] sqlite3_step to select and update the same table




Hello,
        I am trying to select some columns from a table and use that to
update another column in the same table using prepare/step/reset/finalize
methods. However, when I use the update statement while stepping it is
executing the flow 2 times. In order to clarify, I am pasting some pseudo
code

zSql = sqlite3_mprintf("SELECT * FROM table1 WHERE abc=? ;");
if( (rc = sqlite3_prepare_v2(db, zSql, (int)strlen(zSql), &stmt, NULL)) !=
SQLITE_OK ) {
        fprintf(stderr, "SQL error: preparing update statement\n");
        goto error;
}
rc = sqlite3_bind_text(stmt, 1, temp, (int)strlen(temp), NULL);
if( rc != SQLITE_OK ) { goto error;}
/* Execute the query expression */
while(sqlite3_step(stmt) == SQLITE_ROW) {
        ref = sqlite3_column_int(stmt,3);
        printf("Before sql execution %d \n",ref);
        delSql = sqlite3_mprintf("UPDATE table1 SET ghi = %d WHERE def = %d
;",1,ref);
        if( (rc = sqlite3_exec(db, delSql, NULL, NULL, &zErrMsg)) != SQLITE_OK 
) {
sqlite3_free(zErrMsg); goto error; }
        }
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
Output :
Before sql execution 5
Before sql execution 5
Before sql execution 6
Before sql execution 6

As you can see the print statement is being printed 2 times and this code is
not in a loop, apart from the while for stepping. If I replace the update
statement with another select statement I behaves normally i.e. just
executes the query once, prints just once.

Do I have to do something special If I want to do an update on the table I
am selecting from? I am new to SQLITE hope someone can help me resolve my
newbie problem.

Thanks
--
View this message in context: 
http://old.nabble.com/sqlite3_step-to-select-and-update-the-same-table-tp30152284p30152284.html
Sent from the SQLite mailing list archive at Nabble.com.

_______________________________________________
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

Reply via email to