MySQL C ++连接器使用SELECT查询获取字符串 [英] MySQL C++ Connector getting a string with SELECT query

查看:151
本文介绍了MySQL C ++连接器使用SELECT查询获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,在PAWN之前做过,效果很好,但现在我遇到了问题。我试图从MySQL数据库获取密码,稍后会做其他代码,并且我得到一个十六进制代码?如果这是正确的?下面是我得到的一个例子:0x59fcb0。当我重新启动程序/重新编译它时,它会发生变化。我试图谷歌我的问题,但没有得到任何接近。所以我唯一想做的事情就是获得一个字段的值并将其作为一个变量存储......这是我的代码:

I'm new to mysql on c++, did it before on PAWN, worked great, but now I have a problem. I'm trying to get a password from mysql database, gonna do the rest of the code later and I'm getting a hex code? if that's right? Here's an example of what I'm getting: 0x59fcb0. It allways changes when I restart the program/recompile it. I tried to google for my problem, but didn't get anything close. So the only thing I want to do, Is to get a value of a field and store it as a variable... Here's my code:

#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数据库连接选项。

Also, I have changed the mysql database connection options.

推荐答案

您需要从 res 。以下是一个简单的例子,您可以轻松展开:

You need to fetch the actual field from the res. Here's a simple example that you will easily expand:

#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 */

这篇关于MySQL C ++连接器使用SELECT查询获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆