数据库
首页 > 数据库> > 使用C连接到MySQL服务器

使用C连接到MySQL服务器

作者:互联网

我正在尝试使用C与Visual C 2008 Express Edition上的MySQL ODBC 5.1驱动程序连接到MySQL服务器.

我正在遵循MSDN的这些说明:

> SQLConnect
> SQLGetData
> SQLFetch

唯一的区别是我必须将所有SQLCHAR转换为SQLWCHAR,以匹配函数参数,希望这不会影响连接字符串.

每次我连接时都会得到SQL_ERROR作为返回值.
所以我假设连接字符串或连接语句有问题.

我试过了

DNS = TestConnection; UID =用户; PSW =密码

SERVER =本地主机; DRIVER = {MySQL ODBC 5.1驱动程序}; PORT = 3306; UID =用户; PSW =密码; DATABASE = DBO;
和其他类似的连接字符串.

称为TestConnection的DNS与后一个连接字符串具有相同的信息.

架构是dbo,并且有一个名为testfire的表,其中包含以下列规范:

TEST_ID( INT(11), PRIMARY, AUTO INCREMENT)
TEST_STRING( VARCHAR(50) )
TEST_INTEGER( INT(11) )
TEST_FLOAT( FLOAT )
TEST_DATE( DATETIME )

有3行:

  ID    STRING    INT   FLOAT           DATE
------------------------------------------------------
| 1  |  Test 1  |  1  |  0.1  |  2001-01-01 00:00:00 |
| 2  |  Test 2  |  2  |  0.2  |  2002-01-01 00:00:00 |
| 3  |  Test 3  |  3  |  0.3  |  2003-01-01 00:00:00 |
------------------------------------------------------

我试图使用Excel连接检索数据,主要是为了查看驱动程序是否有效. Excel成功检索数据没有问题,因此名为TestConnection的DNS有效,凭据也是如此.

>我做错了什么?
>我应该改变什么?
>是否转换为MYSQLWCHAR *会弄乱连接字符串?
>是否有不同的,可能更好,更有效的方法? (除了类封装,这是我在测试成功后要做的事情)

哦,编译器没有给出任何错误或警告,代码编译并运行没有任何问题.

所以,这是测试代码,它返回“查询执行错误”:

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

using namespace std;

int main(){
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt;
    SQLRETURN retcode;
    HWND desktopHandle = GetDesktopWindow();

    SQLWCHAR OutConnStr[255];
    SQLSMALLINT OutConnStrLen;
    SQLWCHAR szDNS[2048] ={0};

    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

    // Set the ODBC version environment attribute
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
        retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 

        // Allocate connection handle
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); 

             // Set login timeout to 5 seconds
            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);

                // Connect to data source
                retcode = SQLDriverConnect(
                    hdbc, 
                    desktopHandle, 
                    (SQLWCHAR*)"driver=MySQL Server", 
                    _countof("driver=MySQL Server"),
                    OutConnStr,
                    255, 
                    &OutConnStrLen,
                    SQL_DRIVER_PROMPT );

                // Allocate statement handle
                if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); 

                    // Process data
                    retcode = SQLExecDirect(hstmt, (SQLWCHAR*)"SELECT TEST_STRING, TEST_INTEGER, TEST_FLOAT FROM dbo.testfire", SQL_NTS);

                    if (retcode == SQL_SUCCESS) {
                        SQLINTEGER sTestInt, cbTestStr, cbTestInt, cbTestFloat;
                        SQLFLOAT dTestFloat;
                        SQLCHAR szTestStr[200];
                        while (TRUE) {
                            cout<<"Inside loop";
                            retcode = SQLFetch(hstmt);
                            if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
                                cout<<"An error occurred";
                            }
                            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){

                                SQLGetData(hstmt, 1, SQL_C_CHAR, szTestStr, 200, &cbTestStr);
                                SQLGetData(hstmt, 2, SQL_C_ULONG, &sTestInt, 0, &cbTestInt);
                                SQLGetData(hstmt, 3, SQL_C_FLOAT, &dTestFloat, 0,&cbTestFloat);

                                /* Print the row of data */
                                cout<<szTestStr<<endl;
                                cout<<sTestInt<<endl;
                                cout<<dTestFloat<<endl;
                            } else {
                                break;
                            }
                        }
                    }else{
                        cout<<"Query execution error."<<endl;
                        SQLWCHAR       SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH];
                        SQLINTEGER    NativeError;
                        SQLSMALLINT   i, MsgLen;
                        SQLRETURN     rc2;

                        // Get the status records.
                        i = 1;
                        while ((rc2 = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, i, SqlState, &NativeError,
                            Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA) {
                          cout<<SqlState<<endl;
                          cout<<NativeError<<endl;
                          cout<<Msg<<endl;
                          cout<<MsgLen<<endl;
                          i++;
                        }
                    }
                    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                        SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                    }

                    SQLDisconnect(hdbc);
                }else{ 
                    cout<<"Connection error."<<endl;
                }
                SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
            }
        }
        SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }
    system("pause");
    return 0;
}

UPDATE

在使用Mat提供的文档(请参阅下面的注释)中使用SQLDriverConnect的正确参数更新代码(和post)之后,连接可以正常工作.如何在不提示输入DNS名称的情况下执行相同的操作?将窗口句柄设为null并且……?

现在它在SQLExecDirect(hstmt,(SQLWCHAR *)“SELECT TEST_STRING,TEST_INTEGER,TEST_FLOAT FROM dbo.testfire”,SQL_NTS)失败,但是查询是正确的,那么,问题是什么?

返回的确切错误消息是:

Sql State:      42000
Native Error:   1064
Message:        
Message Length: 211

42000: Syntax error or access violation

*StatementText contained an SQL statement that was not preparable or contained a syntax error.
The user did not have permission to execute the SQL statement contained in *StatementText.

那么……这是什么意思?
我怎么能没有许可?
如何生成语法错误,它显然是一个有效的查询?

解决方法:

在Mat的一点帮助下,我能够弄清楚问题是什么,但由于他没有以答案的形式给出它,我将不得不回答它,以便它可以分享给那些拥有同样的问题,也标记为已回答.

所以,我的问题是我无法连接到数据库.正如Mat建议的那样,我应该使用扩展错误信息,称为SQLGetDiagRec,并根据文档修复参数.我花了一点时间来了解SQLGetDiagRec函数是如何工作的,但是一旦我设法将wchar_t转换为char *,我就能看到它产生的错误.

连接尝试给了我错误数据源未找到,并且没有指定默认驱动程序.这给了我一个线索,表明我写了不正确的连接字符串或文本字符串被某种方式误解或损坏.

做一些searching on the net给了我一个洞察,字符串被误解,并修复它我必须使它成为一个文字字符串.当然,把L放在琴弦前解决了!

retcode = SQLDriverConnect(hdbc, 0, 
                           (SQLWCHAR*)L"DSN=TestConnection;SERVER=localhost;UID=user;PWD=password;DRIVER=MySQL Server;", 
                           _countof(L"DSN=TestConnection;SERVER=localhost;UID=user;PWD=password;DRIVER=MySQL Server;"), 
                           OutConnStr, 255, &OutConnStrLen, SQL_DRIVER_COMPLETE);

与此同时,我学会了如何摆脱提示,在纠正初始问题后很容易弄明白.为窗口句柄指定null,将驱动程序完成设置为SQL_DRIVER_COMPLETE,并确保添加连接字符串中所需的所有信息.

因此,我使用SQLExecDirect进行查询的下一个问题是出现语法错误或访问冲突的错误.问题显然与连接字符串相同.当然够了

retcode = SQLExecDirect(hstmt, (SQLWCHAR*)L"SELECT TEST_STRING, TEST_INTEGER, TEST_FLOAT FROM dbo.testfire", SQL_NTS);

工作就像一个魅力.

以下是完整功能的代码:

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

using namespace std;

int main(){
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt;
    SQLRETURN retcode;

    SQLWCHAR OutConnStr[255];
    SQLSMALLINT OutConnStrLen;

    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

    // Set the ODBC version environment attribute
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
        retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 

        // Allocate connection handle
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); 

             // Set login timeout to 5 seconds
            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);

                // Connect to data source
                retcode = SQLDriverConnect(
                    hdbc, 
                    0,
                    (SQLWCHAR*)L"DSN=TestConnection;SERVER=localhost;UID=root;PWD=never140;DRIVER=MySQL Server;", 
                    _countof(L"DSN=TestConnection;SERVER=localhost;UID=root;PWD=never140;DRIVER=MySQL Server;"),
                    OutConnStr,
                    255, 
                    &OutConnStrLen,
                    SQL_DRIVER_COMPLETE );

                // Allocate statement handle
                if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); 

                    // Process data
                    retcode = SQLExecDirect(hstmt, (SQLWCHAR*)L"SELECT TEST_STRING, TEST_INTEGER, TEST_FLOAT FROM dbo.testfire", SQL_NTS);

                    if (retcode == SQL_SUCCESS) {
                        SQLINTEGER sTestInt, cbTestStr, cbTestInt, cbTestFloat, iCount = 1;
                        SQLFLOAT dTestFloat;
                        SQLCHAR szTestStr[200];
                        while (TRUE) {
                            retcode = SQLFetch(hstmt);
                            if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
                                cout<<"An error occurred";
                            }
                            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){

                                SQLGetData(hstmt, 1, SQL_C_CHAR, szTestStr, 200, &cbTestStr);
                                SQLGetData(hstmt, 2, SQL_C_ULONG, &sTestInt, 0, &cbTestInt);
                                SQLGetData(hstmt, 3, SQL_C_DOUBLE, &dTestFloat, 0,&cbTestFloat);

                                /* Print the row of data */
                                cout<<"Row "<<iCount<<":"<<endl;
                                cout<<szTestStr<<endl;
                                cout<<sTestInt<<endl;
                                cout<<dTestFloat<<endl;
                                iCount++;
                            } else {
                                break;
                            }
                        }
                    }else{
                        cout<<"Query execution error."<<endl;
                    }

                    SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                    SQLDisconnect(hdbc);
                }else{ 
                    cout<<"Connection error"<<endl;
                }
                SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
            }
        }
        SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }

        system("pause");
    return 0;
}

只是去展示,即使是最小的东西也可以让一切都失败.

谢谢你的帮助.

标签:c-2,mysql,odbc,driver,visual-c-2008
来源: https://codeday.me/bug/20190530/1185860.html