gpt4 book ai didi

c++ - 消息: [unixODBC][Driver Manager]Can't open lib 'MySQL ODBC 5.1 Driver' : file not foundnSQLSTATE: 01000

转载 作者:行者123 更新时间:2023-11-30 00:11:12 27 4
gpt4 key购买 nike

我想使用 C++ 和 mysql 创建 odbc 连接,并且我已附上我的代码。请帮我纠正我发布的问题标题中的错误。

#include <iostream>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

using namespace std;

void show_error(unsigned int handletype, const SQLHANDLE& handle) {
SQLCHAR sqlstate[1024];
SQLCHAR message[1024];
if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle,
1, sqlstate, NULL, message, 1024, NULL))
cout<<"Message: "<<message<<"SQLSTATE: "<<sqlstate<<endl;
}

int main() {
SQLHANDLE sqlenvhandle;
SQLHANDLE sqlconnectionhandle;
SQLHANDLE sqlstatementhandle;
SQLRETURN retcode;

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
goto FINISHED;

if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,
SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
goto FINISHED;

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
goto FINISHED;

SQLCHAR retconstring[1024];
switch(SQLDriverConnect (sqlconnectionhandle, NULL,
(SQLCHAR*)"DRIVER={MySQL ODBC 5.1 Driver};PORT=3306;SERVER=localhost;DATABASE=market;UID=root;PWD=123;",
SQL_NTS,
retconstring,
1024,
NULL,
SQL_DRIVER_NOPROMPT)){
case SQL_SUCCESS_WITH_INFO:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
break;
case SQL_INVALID_HANDLE:
case SQL_ERROR:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
goto FINISHED;
default:
break;
}

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT,
sqlconnectionhandle, &sqlstatementhandle))
goto FINISHED;

if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle
SQLCHAR*)"select * from employee", SQL_NTS)) {
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
goto FINISHED;
}
else{
char name[64];
char dept[64];

while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS){
SQLGetData(sqlstatementhandle, 1, SQL_C_CHAR, name, 64, NULL);
SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, dept, 64, NULL);
cout<<" "<<name<<" "<<dept<<endl;
}
}

FINISHED:
SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
SQLDisconnect(sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);

}

最佳答案

调用 SQLDriverConnect 时的 Driver 属性是“MySQL ODBC 5.1 Driver”,因此您的 odbcinst.ini 文件中应该有一个条目,如下所示:

[MySQL ODBC 5.1 Driver]
driver = /some/path/to/libxxx.so

您需要检查/some/path/to/libxxx.so 是否存在、可读、可执行,最后可能对其运行 ldd 以确保它所依赖的任何内容都已安装。

您可以使用命令 odbcinst -j 找到 odbcinst.ini 文件。

关于c++ - 消息: [unixODBC][Driver Manager]Can't open lib 'MySQL ODBC 5.1 Driver' : file not foundnSQLSTATE: 01000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com