开发者

Visual C++ ODBC application cannot connect to MySQL database

I'm writing my first database application following a sample program the teacher given, but neither the sample, nor my own program can't connect to the database. (The JDBC sample program can, so the server should be OK).

I have these vars in the class declaration:

SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;  

Here's the constructor of my database handler class, that's where the connection should be made:

DBModule::DBModule(string server, string database)
{
this->server = server; //"localhost" is loaded into it
this->database = database; //"test" is loaded into it, of course it exists on the server

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

command = "DRIVER={MySQL ODBC 3.51 Driver};SERVER="+this->server+";DATABASE="+this->database+";";
//command looks like this now:
//"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;"

ret = SQLDriverConnect(dbc, 开发者_开发知识库NULL, (SQLWCHAR *)command.c_str(), SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);

if (!SQL_SUCCEEDED(ret)) {
    err += CONNECT_DATABASE*DATABASE_UNREACHABLE;
    good = false;
    return;
} else {
    good = true;
}

SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);

command = sysToStd(DBINIT);

SQLPrepare(stmt, (SQLWCHAR *)command.c_str(), SQL_NTS);
ret = SQLExecute(stmt);
}

The ret at SQLDriverConnect gets a -1 value.

I'm using the latest XAMPP as server with all the default settings (so i'm "root" and there is no password). I've tried adding UID=root to the connection string, but it did the same.

Thanks for any help.


You probably do not have MySQL ODBC drivers installed. JDBC works because you need not "install" them: they are some .jar files that can come with Java application. If you will use ODBC then install MySQL ODBC drivers, configure connection in ODBC Manager as System DSN, then from ODBC manager check if it connects to database (most ODBC drivers I know have "test connection" button).

When such test shows you "connected" or similar, then you can test if your application connects. Your connect sting looks like:

  DRIVER={MySQL ODBC 3.51 Driver};SERVER=...;DATABASE....

so according to: http://www.connectionstrings.com/mysql#p30 it looks you are trying to use MySQL Connector/ODBC 3.51

Maybe database is not listening on dafult port?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜