LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2012, 05:28 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
Multithreading with sqlite3 in c


my program is displaying that the
Code:
#include <stdio.h>
#include <pthread.h>
#include "sqlite3.h"
int myInsert(int arg) {
        sqlite3* db = 0;
        sqlite3_stmt* stmt = 0;
        int val = arg;
        char SQL[100];
        int rc;
        rc = sqlite3_open("test.db", &db); /* Open a database named MyDB */
        if (rc != SQLITE_OK) {
                fprintf(stderr, "Thread[%d] fails to open the database\n", val);
                goto errorRet;
        } /* Create the SQL string. If you were using string values, you would need to use sqlite3_prepare() and sqlite3_bind_* to avoid an SQL injection vulnerability. However %d guarantees an integer value, so this use of sprintf is safe. */
        sprintf(SQL, "select sid  from students order by sid", val); /* Prepare the insert statement */
        rc = sqlite3_prepare(db, SQL, -1, &stmt, 0);
        if (rc != SQLITE_OK) {
                fprintf(stderr, "Thread[%d] fails to prepare SQL: %s -> return code %d\n", val, SQL, rc);
                goto errorRet;
        }
        rc = sqlite3_step(stmt);
        if (rc != SQLITE_DONE) {
   fprintf(stderr, "Thread[%d] fails to execute SQL: %s -> return code %d\n", val, SQL, rc); }
        else { printf("Thread[%d] successfully executes SQL: %s\n", val, SQL);
}
        errorRet: sqlite3_close(db);
          return rc;
}
int main(void) {
        pthread_t t[10];
        int i;
        for (i=0; i < 10; i++)
                pthread_create(&t[i], 0,(void *) myInsert, &i); /* Pass the value of i */ /* wait for all threads to finish */ for (i=0; i<10; i++) pthread_join(t[i], 0);
        return 0;
}
i am getting this error
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
Thread[387180012] fails to execute SQL: select sid from students order by sid -> return code 100
 
Old 09-04-2012, 05:38 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
according to this page it means sqlite_step() has another raw to read (so it is not an error). rc = 101 will mean executing finished (no more raws to read).
 
Old 09-04-2012, 05:48 AM   #3
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
according to this page it means sqlite_step() has another raw to read (so it is not an error). rc = 101 will mean executing finished (no more raws to read).

so what should i do..i got your point .but rite now i want to rectify my code so that my thread does not fail..
 
Old 09-04-2012, 05:58 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
it does not fail, it is not an error. This is the expected behavior, so you need to do something like this:
Code:
// repeat until rc = SQLITE_ROW // = 100
// this is not real c code
while (rc = sqlite3_step(stmt)) == SQLITE_ROW
  { do something with the result; }
if rc != SQLITE_DONE ...
 
Old 09-04-2012, 07:29 AM   #5
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
it does not fail, it is not an error. This is the expected behavior, so you need to do something like this:
Code:
// repeat until rc = SQLITE_ROW // = 100
// this is not real c code
while (rc = sqlite3_step(stmt)) == SQLITE_ROW
  { do something with the result; }
if rc != SQLITE_DONE ...


int sqlite3_prepare(
sqlite3 *db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);

i didnt understood : const char **pzTail
 
Old 09-04-2012, 07:44 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
just read the documentation:
Quote:
If pzTail is not NULL then *pzTail is made to point to the first byte past the end of the first SQL statement in zSql. These routines only compile the first statement in zSql, so *pzTail is left pointing to what remains uncompiled.
 
Old 09-04-2012, 07:55 AM   #7
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
just read the documentation:
i read it but didnt got that
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sqlite3 - Invalid driver specified lee_can Linux - Newbie 4 10-31-2011 09:58 PM
urgent help needed with sqlite3 sherifgamal Programming 9 09-10-2010 10:25 PM
PHP + SQLite3 problem estratos Linux - Server 1 04-05-2010 04:41 PM
How do you explain table in SQLite3? novakane Linux - Software 2 03-11-2009 01:11 PM
Sqlite3-3.2.1 execution error Aju Linux - Software 2 10-20-2005 09:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration