Lock_Love_
HotCook
2015-06-30 03:38

关于ODBC中SQLDriverConnect 和 SQLDriverConnectW连接问题

5
  • odbc

Hi,Guys

在采用unixODBC连接Mysql数据库,遇到以下问题:

使用SQLDriverConnect进行连接,正常
使用SQLDriverConnectW进行连接,出现下面的错误,说数据源找不到.

  [unixODBC][Driver Manager]Data source name not found, and no default driver specified

采用的都是同样的配置
环境:
mysql-connector-odbc-5.3.4-linux-glibc2.5-x86-32bit
unixODBC 2.3.2


配置信息

odbcinst.ini

[MySQL]
Driver = /usr/local/lib/libmyodbc5w.so

odbc.ini

[log4cxx]
Driver  = MySQL
Server  = localhost
Port    = 3306
Database= log
User    = root
Password= 123456
Socket  = /var/run/mysqld/mysqld.sock 

C代码 SQLDriverConnectW

int main() {
 SQLHENV env;
 SQLHDBC dbc;
 SQLRETURN ret; /* ODBC API return status */
 SQLWCHAR outstr[1024];
 SQLSMALLINT outstrlen;
 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);
 ret = SQLDriverConnectW(dbc, NULL, (SQLWCHAR *)"DSN=log4cxx;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
 if(ret < 0) {
  printf("asdfasdfasdfsadfsadf\n");
 }
 if (SQL_SUCCEEDED(ret)) {
  printf("Connected\n");
  if (ret == SQL_SUCCESS_WITH_INFO) {
   printf("Driver reported the following diagnostics\n");
   extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  }
  SQLDisconnect(dbc);/* disconnect from driver */
 } else {
  fprintf(stderr, "Failed to connect\n");
  extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
 }
 /* free up allocated handles */
 SQLFreeHandle(SQL_HANDLE_DBC, dbc);
 SQLFreeHandle(SQL_HANDLE_ENV, env);
 return 0;
}

C代码 SQLDriverConnect

int main() {
 SQLHENV env;
 SQLHDBC dbc;
 SQLRETURN ret; /* ODBC API return status */
 SQLCHAR outstr[1024];
 SQLSMALLINT outstrlen;
 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);
 ret = SQLDriverConnect(dbc, NULL, (SQLCHAR *)"DSN=log4cxx;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
 if(ret < 0) {
  printf("asdfasdfasdfsadfsadf\n");
 }
 if (SQL_SUCCEEDED(ret)) {
  printf("Connected\n");
  if (ret == SQL_SUCCESS_WITH_INFO) {
   printf("Driver reported the following diagnostics\n");
   extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  }
  SQLDisconnect(dbc);/* disconnect from driver */
 } else {
  fprintf(stderr, "Failed to connect\n");
  extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
 }
 /* free up allocated handles */
 SQLFreeHandle(SQL_HANDLE_DBC, dbc);
 SQLFreeHandle(SQL_HANDLE_ENV, env);
 return 0;
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答