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

发布于 2024-10-04 11:37:19 字数 1389 浏览 0 评论 0原文

我正在按照老师给出的示例程序编写我的第一个数据库应用程序,但是该示例和我自己的程序都无法连接到数据库。 (JDBC示例程序可以,所以服务器应该没问题)。

我在类声明中有这些变量:

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

这是我的数据库处理程序类的构造函数,这就是应该建立连接的位置:

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);
}

SQLDriverConnect 的 ret 获得 -1 值。

我使用最新的 XAMPP 作为服务器,并使用所有默认设置(所以我是“root”并且没有密码)。我尝试将 UID=root 添加到连接字符串,但效果相同。

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-10-11 11:37:19

您可能没有安装 MySQL ODBC 驱动程序。 JDBC 之所以有效,是因为您不需要“安装”它们:它们是 Java 应用程序附带的一些 .jar 文件。如果您将使用 ODBC,则安装 MySQL ODBC 驱动程序,在 ODBC 管理器中将连接配置为系统 DSN,然后从 ODBC 管理器检查它是否连接到数据库(我知道的大多数 ODBC 驱动程序都有“测试连接”按钮)。

当此类测试显示您“已连接”或类似情况时,您可以测试您的应用程序是否已连接。您的连接字符串看起来像:

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

所以根据: http://www.connectionstrings.com/mysql#p30 看起来您正在尝试使用 MySQL Connector/ODBC 3.51

也许数据库没有侦听 dafult 端口?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文