MySQL C++连接器如何通过SELECT查询获取一个字符串

4

我是C++中的mysql新手,之前在PAWN上做过,效果很好,但现在遇到了一个问题。 我试图从mysql数据库中获取密码,稍后会执行其余的代码,但我得到了一个十六进制代码? 如果这是正确的话? 这是我得到的例子:0x59fcb0。每次重新启动程序/重新编译时都会更改。我尝试谷歌我的问题,但没有找到与此相似的东西。所以我想做的唯一一件事就是获取字段的值并将其存储为变量...这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <iostream>
#include <string>

#include <windows.h>
#include <mysql/mysql.h>

using namespace std;

static char *opt_host_name = "host"; /* HOST */
static char *opt_user_name = "user"; /* USERNAME */
static char *opt_password = "pass"; /* PASSWORD */
static unsigned int opt_port_num = 3306; /* PORT */
static char *opt_socket_name = NULL; /* SOCKET NAME, DO NOT CHANGE */
static char *opt_db_name = "database name"; /* DATABASE NAME */
static unsigned int opt_flags = 0; /* CONNECTION FLAGS, DO NOT CHANGE */

int main ()
{
    MYSQL *conn; /* pointer to connection handler */
    MYSQL_RES *res; /* holds the result set */
    MYSQL_ROW row;

    /* INITIALIZE CONNECTION HANDLER, DO NOT CHANGE */
    conn = mysql_init (NULL);

    /* THIS CONNECTS TO SERVER, DO NOT CHANGE ANYTHING HERE */
    mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
    opt_db_name, opt_port_num, opt_socket_name, opt_flags);
    /* show tables in the database (test for errors also) */
    mysql_query(conn, "SELECT Password FROM Users WHERE Name = 'MY_NICKNAME'");
    res = mysql_store_result(conn);
    cout << "Password is: \n";
    cout << res << endl;

    /* disconnect from server */
    mysql_close (conn);

    system("pause");
    return 0;
} 
/* end main function */

另外,我已经更改了mysql数据库连接选项。


看起来你正在使用MySQL C API,而不是C++连接器。 - Barmar
你需要调用mysql_fetch_XXX函数之一来从结果中获取行。 - Barmar
1个回答

8
您需要从res中获取实际字段。以下是一个简单的示例,您可以轻松扩展:
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <iostream>
#include <string>

#include <windows.h>
#include <mysql/mysql.h>

using namespace std;

static char *opt_host_name = "host"; /* HOST */
static char *opt_user_name = "user"; /* USERNAME */
static char *opt_password = "pass"; /* PASSWORD */
static unsigned int opt_port_num = 3306; /* PORT */
static char *opt_socket_name = NULL; /* SOCKET NAME, DO NOT CHANGE */
static char *opt_db_name = "database name"; /* DATABASE NAME */
static unsigned int opt_flags = 0; /* CONNECTION FLAGS, DO NOT CHANGE */

int main ()
{
    MYSQL *conn; /* pointer to connection handler */
    MYSQL_RES *res; /* holds the result set */
    MYSQL_ROW row;



    /* INITIALIZE CONNECTION HANDLER, DO NOT CHANGE */
    conn = mysql_init (NULL);

    /* THIS CONNECTS TO SERVER, DO NOT CHANGE ANYTHING HERE */
    mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
    opt_db_name, opt_port_num, opt_socket_name, opt_flags);
    /* show tables in the database (test for errors also) */
    mysql_query(conn, "SELECT Password FROM Users WHERE Name = 'MY_NICKNAME'");
    res = mysql_store_result(conn);


    // get the number of the columns
    int num_fields = mysql_num_fields(res);
    // Fetch all rows from the result
    while ((row = mysql_fetch_row(res)))
    {
       // Print all columns
       for(int i = 0; i < num_fields; i++)
       {
           // Make sure row[i] is valid!
           if(row[i] != NULL)
                cout << row[i] << endl;
           else
                cout << "NULL" << endl;

           // Also, you can use ternary operator here instead of if-else
           // cout << row[i] ? row[i] : "NULL" << endl;
       }
    }

    // DON'T FORGET TO CLEAN RESULT AFTER YOU DON'T NEED IT 
    // ANYMORE

    if(res != NULL)
       mysql_free_result(res);

    /* disconnect from server */
    mysql_close (conn);

    system("pause");
    return 0;
} 
/* end main function */

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接