http://www.sqlite.org/faq.html#q6 seems misleading when also reading http://www.sqlite.org/threadsafe.html
Looks like you need to finalize() inside your mutex boundary according to the FAQ. But, if you use serialized mode the other page says "no restriction". Make sure you use serialized mode and it appears you don't need any mutexes and you should be able to reuse prepared statements like I showed you. You won't get 2 requests at the same time to the database as they will be serialized. Michael D. Black Senior Scientist Advanced Analytics Directorate Advanced GEOINT Solutions Operating Unit Northrop Grumman Information Systems From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on behalf of Christian [siriu...@gmx.de] Sent: Friday, October 28, 2011 10:08 AM To: General Discussion of SQLite Database Subject: EXT :Re: [sqlite] Segmentation Fault on SQLITE3_exex Many thanks for your instant help so far! I will run a few tests actually I use on single class instantiation and each thread has an pointer to the object. The code I put below is incomplete but describes the standard behavoir in all the database methods, I don't want to give full access to the db, only some functionalities should be accessable. So the structure is somehow like this: Main: Creates an instance to the DB and opens it. All Threads using the database are started here and get a pointer to this single instance. Within the instance only methods like: int getAttributeID (int id) are accessable. To ensure that there are not 2 requests at the same time I manually put mutexes at the beginning of each function which will block until the mutex is available. At the end of each function or before I return from the function I release the mutex in order to give access to the next request. Correct me if I'm wrong but I guess there could not be any call to a function at the same time beside I miss to set/release a mutex in one of these functions. Best regards, Chris -------- Original-Nachricht -------- > Datum: Fri, 28 Oct 2011 12:51:21 +0000 > Von: "Black, Michael (IS)" <michael.bla...@ngc.com> > An: General Discussion of SQLite Database <sqlite-users@sqlite.org> > Betreff: Re: [sqlite] Segmentation Fault on SQLITE3_exex > 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: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on > behalf of Christian [siriu...@gmx.de] > Sent: Friday, October 28, 2011 3:07 AM > To: General Discussion of SQLite Database; sqlite-users@sqlite.org > 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 > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users -- NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie! Jetzt informieren: http://www.gmx.net/de/go/freephone _______________________________________________ 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