2013-08-24 50 views
1

我是C++的新手,在PAWN之前做過,效果很好,但現在我遇到了問題。我試圖從MySQL數據庫獲取密碼,稍後會做其他代碼,並且我得到一個十六進制代碼?如果這是正確的?下面是我得到的一個例子:0x59fcb0。當我重新啓動程序/重新編譯它時,它會發生變化。我試圖谷歌我的問題,但沒有得到任何接近。所以我想唯一能做的,就是拿到了字段的值,並將其存儲爲一個變量...以下是我的代碼:MySQL C++連接器使用SELECT查詢得到一個字符串

#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數據庫的連接選項。

+0

看起來您正在使用MySQL C API,而不是C++連接器。 – Barmar

+0

您需要調用其中一個'mysql_fetch_XXX'函數來獲取結果中的行。 – Barmar

回答

2

您需要從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 */ 
+0

這真的很有幫助,謝謝 – KlaudijusM