What you're showing should basically work as long it's not a single class
instantiation being used by multiple threads.
Also, your missing a bind on the 2nd statement.
And you're not retrieving the results of the query (I assume you left that out
for brevity).
What I would do is this to maximize speed...you keep the prepared statement
around for each query and reuse it.
Somebody else may have a better idea. But hopefully this shows how to "reuse"
a prepared statement.
db = MyDBClass("mydb.sqlite"); // opens database
run_threads(); // you did say you have threads so kick them off
// here's what a thread would look like -- note that I don't have complete
error checking in here for brevity
thread()
{
pStmt1=db->getPreparedStatement("SELECT * from model where id=?");
pStmt2=db->getPreparedStatement("SELECT * from type where id=?");
while (1) { // or whatever loop you're running
// 1st query
sqlite3_bind_int (pStmt1, 1, dd.deviceIndex);
doStmt(pStmt1);
int myIndex=sqlite3_column_int(pStmt1,0);
// 2nd query
sqlite3_bind_int (pStmt2, 1, dd.deviceIndex);
doStmt(pStmt2);
int myType=sqlite3_column_int(pStmt2,0);
}
sqlite3_finalize(pStmt1);
sqlite3_finalize(pStmt2);
}
doStmt(sqlite3_stmt *pStmt)
{
sqlite3_reset(pStmt); // we reset on the way in so we can use the results
AFTER we step outside this function
sqlite3_step(pStmt);
}
Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Advanced GEOINT Solutions Operating Unit
Northrop Grumman Information Systems
From: [email protected] [[email protected]] on
behalf of Christian [[email protected]]
Sent: Friday, October 28, 2011 3:07 AM
To: General Discussion of SQLite Database; [email protected]
Subject: EXT :Re: [sqlite] Segmentation Fault on SQLITE3_exex
First of all thanks for your detailed reply! According to the documentation its
not recommended to reuse a preparedStatement after finalizing it. So my initial
guess to do something like this:
sqlite3_stmt *preparedStatement = 0;
static char command [1024];
sprintf (command, "SELECT * from model where id=?");
if (sqlite3_prepare_v2 (db, command, strlen(command), &preparedStatement, 0) !=
SQLITE_OK)
{
LOG_MESSAGE ("SQLite Error: %s error=%d\n", sqlite3_errmsg (db),
sqlite3_errcode (db));
sqlite3_finalize (preparedStatement);
return FALSE;
}
sqlite3_bind_int (preparedStatement, 1, dd.deviceIndex);
success = sqlite3_step(preparedStatement);
//read result
if ((retVal= sqlite3_finalize(preparedStatement)) != SQLITE_OK){
LOG_MESSAGE ("SQLite Error: %s error=%d\n", sqlite3_errmsg (db),
sqlite3_errcode (db));
}
else
preparedStatement=0;
sprintf (command, "SELECT * FROM type WHERE id=%d", id);
if (sqlite3_prepare_v2 (db, command, strlen(command), &preparedStatement, 0) !=
SQLITE_OK)
{
LOG_MESSAGE ("SQLite Error: %s error=%d\n", sqlite3_errmsg (db),
sqlite3_errcode (db));
sqlite3_finalize (preparedStatement);
return FALSE;
}
if ((success = sqlite3_step(preparedStatement)) != SQLITE_ROW)
{
LOG_MESSAGE ("SQLite Error: %s error=%d\n", sqlite3_errmsg (db),
sqlite3_errcode (db));
sqlite3_finalize (preparedStatement);
return FALSE;
}
//read result
sqlite3_finalize (preparedStatement);
return TRUE;
Am I right that this is not possible? So I have to use one sqlite3_stmt for
each prepare?
Best regards,
Chris.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users