Re: [sqlite] Prepare statement in separate function
enjoythe...@hushmail.com wrote: > I was allocating memory because I wanted to prepare the statement > in a separate function. Just prepare the statement, and return sqlite3_stmt* by value. You are not allocating memory when returning, say, an int, right? sqlite3_stmt* is comparable in size, and can be treated similarly. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Prepare statement in separate function
hello martin, I was allocating memory because I wanted to prepare the statement in a separate function. After all I have changed the whole implementation design to something less awkward :) greetings, john On Tue, 11 Oct 2011 11:46:01 +0200 Martin Engelschalkwrote: >Hello John, > >why do you malloc() your DB- and Statement handle? > >I declare a > >sqlite3* pDB; >sqlite3_stmt* pStmnt; > >then open the database with > >int nRet = sqlite3_open("MyDatabaseName", ); > >and prepare a statement using > >nRet = sqlite3_prepare_v2(pDB, "insert .", -1, , >); > >no need to malloc (or free) anything, and passing pDB or pStmnt to > >functions is easy. > >Martin > > >Am 11.10.2011 11:27, schrieb enjoythe...@hushmail.com: >> hello list, >> >> I have a question regarding prepared statements. I'm fairly new >to >> sqlite3 and it's already pretty late, so if I'm overlooking >> something obvious please forgive me :) I'm on Windows 7 x64 and >> using sqlite-3070800 (amalgamation). >> >> I'm trying to prepare an insert statement, that I will reuse >many >> times using bound parameters. In a short test application, if I >> prepare the statement within the main function everything is >fine: >> >> CODE >> ... >> int rc=0; >> pDb = (sqlite3*) malloc (sizeof(sqlite3*)); >> stmt = (sqlite3_stmt*) malloc (sizeof(sqlite3_stmt*)); >> ... >> >> if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1,, NULL)) != >> SQLITE_OK){ >> /* ... error ... */ >> } >> >> sqlite3_step(stmt); >> ... >> \CODE >> >> However, if I try throw this code in a separate function like >this, >> >> CODE >> int prepareStatement(sqlite3_stmt* stmt, sqlite3* pDb){ >> >> int rc=0; >> >> if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1,, NULL)) >!= >> SQLITE_OK){ >> /* ... error ... */ >> } >> return 0; >> } >> \CODE >> >> it fails as soon as I call sqlite3_step(stmt) afterwards. The >> debugger stops at: >> >> sqlite3_mutex_enter(db->mutex); >> >> At first I thought I was doing smthg wrong during memory >> allocation, but everything seems to be fine there. I'm sure I'm >> overlooking an obvious mistake. Maybe somebody can give me a >hint! >> >> thanks, >> John >> >> ___ >> 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 ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Prepare statement in separate function
Hello John, why do you malloc() your DB- and Statement handle? I declare a sqlite3* pDB; sqlite3_stmt* pStmnt; then open the database with int nRet = sqlite3_open("MyDatabaseName", ); and prepare a statement using nRet = sqlite3_prepare_v2(pDB, "insert .", -1, , ); no need to malloc (or free) anything, and passing pDB or pStmnt to functions is easy. Martin Am 11.10.2011 11:27, schrieb enjoythe...@hushmail.com: hello list, I have a question regarding prepared statements. I'm fairly new to sqlite3 and it's already pretty late, so if I'm overlooking something obvious please forgive me :) I'm on Windows 7 x64 and using sqlite-3070800 (amalgamation). I'm trying to prepare an insert statement, that I will reuse many times using bound parameters. In a short test application, if I prepare the statement within the main function everything is fine: CODE ... int rc=0; pDb = (sqlite3*) malloc (sizeof(sqlite3*)); stmt = (sqlite3_stmt*) malloc (sizeof(sqlite3_stmt*)); ... if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1,, NULL)) != SQLITE_OK){ /* ... error ... */ } sqlite3_step(stmt); ... \CODE However, if I try throw this code in a separate function like this, CODE int prepareStatement(sqlite3_stmt* stmt, sqlite3* pDb){ int rc=0; if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1,, NULL)) != SQLITE_OK){ /* ... error ... */ } return 0; } \CODE it fails as soon as I call sqlite3_step(stmt) afterwards. The debugger stops at: sqlite3_mutex_enter(db->mutex); At first I thought I was doing smthg wrong during memory allocation, but everything seems to be fine there. I'm sure I'm overlooking an obvious mistake. Maybe somebody can give me a hint! thanks, John ___ 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
Re: [sqlite] Prepare statement in separate function
hello again, ok, right after posting to the list, I have found the mistake. Obviously it needs to be: sqlite3_stmt** stmt = (sqlite3_stmt**) malloc (sizeof(sqlite3_stmt*)); thanks list! :) On Tue, 11 Oct 2011 11:27:41 +0200 enjoythe...@hushmail.com wrote: >hello list, > >I have a question regarding prepared statements. I'm fairly new to > >sqlite3 and it's already pretty late, so if I'm overlooking >something obvious please forgive me :) I'm on Windows 7 x64 and >using sqlite-3070800 (amalgamation). > >I'm trying to prepare an insert statement, that I will reuse many >times using bound parameters. In a short test application, if I >prepare the statement within the main function everything is fine: > >CODE >... >int rc=0; >pDb = (sqlite3*) malloc (sizeof(sqlite3*)); >stmt = (sqlite3_stmt*) malloc (sizeof(sqlite3_stmt*)); >... > >if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1, , NULL)) != >SQLITE_OK){ > /* ... error ... */ >} > >sqlite3_step(stmt); >... >\CODE > >However, if I try throw this code in a separate function like >this, > >CODE >int prepareStatement(sqlite3_stmt* stmt, sqlite3* pDb){ > > int rc=0; > > if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1, , NULL)) != > >SQLITE_OK){ > /* ... error ... */ > } > return 0; >} >\CODE > >it fails as soon as I call sqlite3_step(stmt) afterwards. The >debugger stops at: > >sqlite3_mutex_enter(db->mutex); > >At first I thought I was doing smthg wrong during memory >allocation, but everything seems to be fine there. I'm sure I'm >overlooking an obvious mistake. Maybe somebody can give me a hint! > >thanks, >John > >___ >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
[sqlite] Prepare statement in separate function
hello list, I have a question regarding prepared statements. I'm fairly new to sqlite3 and it's already pretty late, so if I'm overlooking something obvious please forgive me :) I'm on Windows 7 x64 and using sqlite-3070800 (amalgamation). I'm trying to prepare an insert statement, that I will reuse many times using bound parameters. In a short test application, if I prepare the statement within the main function everything is fine: CODE ... int rc=0; pDb = (sqlite3*) malloc (sizeof(sqlite3*)); stmt = (sqlite3_stmt*) malloc (sizeof(sqlite3_stmt*)); ... if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1, , NULL)) != SQLITE_OK){ /* ... error ... */ } sqlite3_step(stmt); ... \CODE However, if I try throw this code in a separate function like this, CODE int prepareStatement(sqlite3_stmt* stmt, sqlite3* pDb){ int rc=0; if((rc=sqlite3_prepare_v2(pDb, INSERT_STMT , -1, , NULL)) != SQLITE_OK){ /* ... error ... */ } return 0; } \CODE it fails as soon as I call sqlite3_step(stmt) afterwards. The debugger stops at: sqlite3_mutex_enter(db->mutex); At first I thought I was doing smthg wrong during memory allocation, but everything seems to be fine there. I'm sure I'm overlooking an obvious mistake. Maybe somebody can give me a hint! thanks, John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users