設置 | 登錄 | 註冊

目前共有15篇帖子。

【方法】VS2012下连接64位的MySQL数据库

11樓 巨大八爪鱼 2016-2-29 22:47
但是需要注意的是,建立控制台程序时最好建立空项目,否则可能会无法编译64位的程序。
12樓 巨大八爪鱼 2016-2-29 22:49
VS2012状态栏上应该选择x64平台:

13樓 巨大八爪鱼 2016-2-29 22:49

回復12樓 @巨大八爪鱼 的內容:

VS2012状态栏上应该选择x64平台:
应该是工具栏!
14樓 巨大八爪鱼 2016-2-29 23:17
【在控制台中显示UTF-8字符串的方法】
#include <iostream>
#include <mysql/mysql.h>
#include <Windows.h>

using namespace std;

#define DB_PASSWORD "密码"

int main(void)
{
    MYSQL conn;
    mysql_init(&conn);
    if (!mysql_real_connect(&conn, "127.0.0.1", "root", DB_PASSWORD, "super", NULL, NULL, NULL))
    {
        cout << "无法连接数据库" << endl;
        return 1;
    }
    mysql_set_character_set(&conn, "utf8");

    char *sql = "SELECT * FROM role ORDER BY ID";
    mysql_query(&conn, sql);

    MYSQL_RES *rs = mysql_store_result(&conn);
    MYSQL_ROW row;
    int i;
    for (i = 0; row = mysql_fetch_row(rs); i++)
    {
        // 将row[1]从UTF8转换到UTF16,然后再转换为ANSI
        int n = MultiByteToWideChar(CP_UTF8, NULL, row[1], -1, NULL, NULL);
        wchar_t *wstr = new wchar_t[n];
        MultiByteToWideChar(CP_UTF8, NULL, row[1], -1, wstr, n);
        n = WideCharToMultiByte(CP_ACP, NULL, wstr, -1, NULL, NULL, NULL, NULL);
        char *str = new char[n];
        WideCharToMultiByte(CP_ACP, NULL, wstr, -1, str, n, NULL, NULL);

        cout << "第" << row[0] << "条记录: ";
        cout << str << endl;

        delete[] str;
        delete[] wstr;
    }
    mysql_free_result(rs);

    mysql_close(&conn);
    system("pause");
    return 0;
}
【运行结果】

15樓 巨大八爪鱼 2016-2-29 23:19
如果要在窗口程序中显示UTF8字符串,只需将其转换为UTF16(wchar_t)就行了,无需再转换为ANSI

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。