Hello,
the problem, which cases the bug is the lifetime of the objects. A
tntdb::Statement must be released before the tntdb::Connection, where
it was prepared. It is not quite obvious here.
The variables conn and st were declared outside the loop. Inside the
loop you first create a connection and then prepare the statement. In
the next iteration a new connection is created. The old one is released
automatically, since it is the only reference. Here the
tntdb::Statement is still held. In the next prepare, the previous
statement is released, but the corresponding connection do not exist
any more.
The solution is to put the tntdb::Statement st inside the loop:
for( int i = 1;; i++) {
cp = ( i % 2) ? "sqlite:testdb_2.db" : "postgresql:dbname=test2";
std::cout << "connecting to " << cp << std::endl;
conn = tntdb::connect( cp);
std::cout << "going to prepare statement" << std::endl;
tntdb::Statement st = conn.prepare( "UPDATE y SET x = x + 1");
std::cout << "going to execute statement" << std::endl;
st.execute();
Tommi and many greetings from the LinuxTag 2011 in Berlin.
Am Fri, 13 May 2011 12:44:46 +0200
schrieb Michael Tomuschat <[email protected]>:
> Hello,
>
> I habve problems switching databases inside a program This small
> example gives an segmentation fault when switching from en existing
> connection tom SQLite3:
>
>
> #include <iostream>
> #include <string.h>
>
> #include <tntdb/connection.h>
> #include <tntdb/connect.h>
> #include <tntdb/statement.h>
>
> int main( int argc, char **argv) {
> std::string line;
> const char* cp;
> tntdb::Connection conn;
> tntdb::Statement st;
>
> for( int i = 1;; i++) {
> cp = ( i % 2) ? "sqlite:testdb_2.db" :
> "postgresql:dbname=test2"; std::cout << "connecting to " << cp <<
> std::endl; conn = tntdb::connect( cp);
>
> std::cout << "going to prepare statement" << std::endl;
> st = conn.prepare( "UPDATE y SET x = x + 1");
> std::cout << "going to execute statement" << std::endl;
> st.execute();
> std::cout << "FINISHED" << std::endl;
>
> std::getline( std::cin, line);
> if( line[ 0] == 'q')
> break;
> }
> }
>
> Output:
> ========================
> connecting to sqlite:testdb_2.db
> going to prepare statement
> going to execute statement
> FINISHED
>
> connecting to postgresql:dbname=test2
> going to prepare statement
> going to execute statement
> FINISHED
>
> connecting to sqlite:testdb_2.db
> going to prepare statement
> /bin/bash: Zeile 1: 20902 Speicherzugriffsfehler xx
>
>
> Thanks for any help
>
> Michael
>
>
------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general