MySQL Connector efficient use

I am fairly new to SQL and I want to make sure I am using the connector as efficiently as possible Below is a function which I use to connect to a MySQL database using MySQL connector. It works well, however I have a lot of other functions which perform very similar tasks to different tables or by referencing different columns. Two key points I am trying to understand are:

1. If I were to write a class for all of these functions, how much of this could I put into the constructor, destructor? do I need to re-initialize everything, every time I make a call?

2. When researching how to get variables into SQL command lines, my current method as well as prepared_statement.h were the two methods I found. I had trouble getting the latter to work so I stuck with the current method. Is there any reason I should give prepared_statement.h another try?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
string pullData_USERS( string uid, string column ){
    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        driver = get_driver_instance();
        con = driver->connect( ADDRESS,USERNAME,PASSWORD );
        con->setSchema( DATABASE );
        stmt = con->createStatement();
        ostringstream strstr;
        strstr << "SELECT "<< column <<" FROM USERS "<< " WHERE UID='" << uid << "'";
        string s = strstr.str();
        res = stmt->executeQuery(s.c_str());
        while( res->next())
			s = res->getString(column);
		return s;
        } catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        }			
    return NULL;
}
Topic archived. No new replies allowed.