简体   繁体   English

Visual C ++ ODBC应用程序无法连接到MySQL数据库

[英]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). (可以使用JDBC示例程序,因此服务器应该可以)。

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. SQLDriverConnect的ret值为-1。

I'm using the latest XAMPP as server with all the default settings (so i'm "root" and there is no password). 我使用最新的XAMPP作为具有所有默认设置的服务器(所以我是“ root”,没有密码)。 I've tried adding UID=root to the connection string, but it did the same. 我尝试将UID = root添加到连接字符串中,但是这样做相同。

Thanks for any help. 谢谢你的帮助。

You probably do not have MySQL ODBC drivers installed. 您可能没有安装MySQL ODBC驱动程序。 JDBC works because you need not "install" them: they are some .jar files that can come with Java application. JDBC之所以有效,是因为您不需要“安装”它们:它们是Java应用程序可以附带的一些.jar文件。 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). 如果要使用ODBC,然后安装MySQL ODBC驱动程序,则在ODBC管理器中将连接配置为System DSN,然后从ODBC管理器中检查其是否连接到数据库(我知道的大多数ODBC驱动程序都有“测试连接”按钮)。

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 所以根据: http : //www.connectionstrings.com/mysql#p30看来您正在尝试使用MySQL Connector / ODBC 3.51

Maybe database is not listening on dafult port? 也许数据库没有在dafult端口上监听?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM