I started learning c++ just a while ago, and I'm currently trying to create a simple mysql db connection. Although I have a web developer background setting up everything was pretty hard because of no c++ knowledge and how things work. Anyway I managed to install mysql++ and I got a piece of code from a tutorial that looks like this:

#include </usr/local/include/mysql++/mysql++.h>
#include </usr/local/include/mysql++/cmdline.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const char* db      = "backseat";
    const char* server  = "localhost";
    const char* user    = "root";
    const char* pass    = "root";

    mysqlpp::Connection conn(true);

    if ( conn.connection(db, server, user, pass) ) 
    {
        mysqlpp::Query query = conn.query('SHOW TABLES');
        if ( mysqlpp::StoreQueryResult res = query.store() )
        {
            cout << "We have: " << endl;
            for (size_t i=0; i < res.num_rows(); i++)
            {
                cout << '\t' << res[i][0] << endl;
            }
        }
        else
        {
            cerr << "Failed to get item list: " << query.error() << endl;
            return 1;
        }

        return 0;
    }
    else
    {
        cerr << "DB connection failed: " << conn.error() << endl;
        return 1;
    }
}

which is supposed to work but g++ gives me the following output:

syd@whitebook:~/Desktop$ g++ sql.cpp -o sql -DMYSQLPP_MYSQL_HEADERS_BURIED
sql.cpp:19:37: warning: character constant too long for its type [enabled by default]
sql.cpp: In function ‘int main()’:
sql.cpp:17:12: error: ‘class mysqlpp::Connection’ has no member named ‘connection’
sql.cpp:19:50: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
/usr/local/include/mysql++/connection.h:251:8: error:   initializing argument 1 of ‘mysqlpp::Query mysqlpp::Connection::query(const char*)’ [-fpermissive]
syd@whitebook:~/Desktop$ 

What am I doing wrong here? Also would you suggest mysql++ or something else?

PS: tutorial - guide taken from here: http://my-problem-solved.blogspot.gr/2010/12/installing-mysql-server-and-mysql-apl.html

有帮助吗?

解决方案

You have a typo (t missing)

mysqlpp::Connecion conn(true);

should be

mysqlpp::Connection conn(true);
               ^
               ^

You may have other errors when you correct this but this is the cause of your compilation errors. If you read the compilation error message very carefully you can see that it actually echos back the mistyped class name Connecion.

In light of your rewriting the question, I believe the function you want is called

 conn.connect(db, server, user, pass)

This certainly works for me when I use mysql++. You can also use

conn.connected();

to see if your connection is OK;

Also

mysqlpp::Query query = conn.query("SHOW TABLES");

instead of

mysqlpp::Query query = conn.query('SHOW TABLES');

Or you can use the << operator

Query query = conn.query();
query << "SHOW TABLES";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top