How to Keep a MySqlpp connection alive

Hi,

I'm wanting to start a database connection at the start of an app and keep it open for all methods and classes that I'm #including in my project.

First I went about it by creating the connection in the Main source file outside of any class declarations and then declaring it external in other source files.

// Main cpp file
#include <mysql++>

mysql::Connection * conn;

// Class definitions follow, I open the connection in Main()
if (!(conn.connect("dbName", "localhost", "user", "pwd"))) {
...

This works ok within the same source file and although it compiles, the connection is not open in any function defined outside the file (in another source file).

Second method I tried was to pass the mysql::Connection * conn as a parameter: // Call MyFn from a method in Main source file:
MyFn(this, &conn);

connection appears alive but but the app crashes when I execute the query:

// Function def in Another source file
void myFn(wxFrame *frame, Connection *conn)
{
mysqlpp::Query query = conn.query(); <--- THIS is probably the cause !?
// set up query code...
// execute
mysqlpp::StoreQueryResult res = query.store(); // <--- Exception happens here

Can anyone help me please ?
Topic archived. No new replies allowed.