Re: [sqlite] Using prepare, step, finalize and hadling BUSY answers

2006-07-21 Thread Jay Sprenkle

On 7/21/06, Daniel van Ham Colchete <[EMAIL PROTECTED]> wrote:


I'm having problems understanding the SQLite docs. At the 'C/C++
Interface for SQLite Version 3' it says that sqlite3_exec is a wrapper
to 'prepare, finalize, reset' without a step. But a little bit down the
document it says you should use and step. But again, what if I don't
want to wait the busy state anymore? Should I use finalize or interrupt
and then finalize?



Here's some example code:

sqlite3*db;

// connect to database
if ( sqlite3_open( "test.db", &db ) )
  throw "Can't open database";

char* sql;

// two forms of the same sql
sql = "SELECT one.test1, two.test2"
 " FROM one"
 " INNER JOIN two ON one.id = two.id"
 ;
sqlite3_stmt*   pStmt;

if ( sqlite3_prepare( db, sql, strlen(sql), &pStmt, NULL ) != SQLITE_OK )
  {
 string str = "Cannot prepare sql: ";
 str += sql[t];
 str += ", Error: ";
 str += sqlite3_errmsg(db);
 throw str.c_str();
  }

bool Loop = true;
while ( Loop )
  switch ( sqlite3_step( pStmt ) )
 {
case SQLITE_ROW:
   // retrieve the results
   char* p = (char *) sqlite3_column_text( pStmt, 0 );
   string test1  = string( p ? p : "" );

   p = (char *) sqlite3_column_text( pStmt, 1 );
   string test2 = string( p ? p : "" );

   break;
case SQLITE_DONE:
   Loop = false;
   break;
case SQLITE_BUSY:
case SQLITE_LOCKED:
default:
   string str = "Cannot execute sql: ";
   str += sql[t];
   str += ", Error: ";
   str += sqlite3_errmsg(db);
   throw str.c_str();
   break;
 }

// clean up when finished
sqlite3_finalize( pStmt );

sqlite3_close( db );


--
SqliteImporter and SqliteReplicator: Command line utilities for Sqlite
http://www.reddawn.net/~jsprenkl/Sqlite

Cthulhu Bucks!
http://www.cthulhubucks.com


[sqlite] Using prepare, step, finalize and hadling BUSY answers

2006-07-21 Thread Daniel van Ham Colchete
Hello everyone,

I'm new on this list and I have only a few months of experience with
SQLITE. I use SQLITE 3.3.6 for Linux with C++.

I'm having a few problems with locking. But before describing the
problem itself, I would like to check if I'm doing something wrong.

That's what I do when I try to INSERT something in my database:

PREPARE
BIND
EC = STEP
while EC = BUSY {
  TRY AGAIN 10 TIMES; // SQLITE is set to sleep 1 sec if BUSY.
  IF TIME > 10 { BREAK; }
}

And then I do some other selects and inserts. Now I read that there is
another function I should be using: sqlite3_finalize. From what I
understood I should be doing:

PREPARE
BIND
EC = STEP
while EC = BUSY {
  TRY AGAIN 10 TIMES; // SQLITE is set to sleep 1 sec if BUSY.
  IF TIME > 10 { BREAK; }
}
FINALIZE

Am I right?

Question: what if a PREPARE returns BUSY? What should I do? Why would a
PREPARE return BUSY?

I'm having problems understanding the SQLite docs. At the 'C/C++
Interface for SQLite Version 3' it says that sqlite3_exec is a wrapper
to 'prepare, finalize, reset' without a step. But a little bit down the
document it says you should use and step. But again, what if I don't
want to wait the busy state anymore? Should I use finalize or interrupt
and then finalize?

Thank you very much for your help!

Best regards,
Daniel Colchete