Mysql连接器C.

时间:2016-04-11 20:22:48

标签: c++ database connector

我不懂英文。我在翻译的帮助下写信给你。 我有个问题。我6天都解决不了。 我使用MySQL Connector C API和c ++连接到数据库。 问题是编译的应用程序在运行查询时显示错误。 此请求包含西里尔文。字母(А,Б,В,Г,Д......) - 俄文字母。

以下是我的应用的示例实现:

0

1366
Incorrect string value: '\xCF\xE5\xF2\xF0' for column 'name' at row 1

现在更详细:

的my.ini

[client]
port        = 3306
default-character-set = utf8
character-sets-dir    = "C:\\Database\\mysql\\share\\charsets"

[mysql]
default-character-set = utf8
character-sets-dir    = "C:\\Database\\mysql\\share\\charsets"

[mysqld]
basedir = "C:\\Database\\mysql"
datadir = "C:\\Database\\data"
character-set-server = utf8
character-sets-dir = "C:\\Database\\mysql\\share\\charsets"
collation-server = utf8_general_ci
init-connect = "SET NAMES utf8"
port = 3306
log-error = errors.err

源代码:

#include <string.h>
#include <mysql.h>

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    char *host = "127.0.0.1";
    char *login = "root";
    char* pass = "12345";
    char* dbname = "documents";
    unsigned int port = 3306;

    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    conn = mysql_init(NULL);

    if (!(mysql_real_connect(conn, host, login, pass, dbname, port, NULL, 0)))
    {
        cout << "Error";
        _getch();
        exit(1);
    }

    mysql_query(conn, "SET NAMES utf8");

    cout << mysql_errno(conn) << endl;
    cout << mysql_error(conn) << endl;


    mysql_query(conn, "INSERT INTO person VALUES('Петр')");
    //mysql_query(conn, "INSERT INTO person(name) VALUES ('Petr')");

    cout << mysql_errno(conn) << endl;
    cout << mysql_error(conn) << endl;

    mysql_close(conn);

    _getch();
    return 0;
}

这里注意这一行:

mysql_query(conn, "INSERT INTO person(name) VALUES ('Petr')");

行成功,结果:

+------+
| name |
+------+
| Petr |
+------+

线

mysql_query(conn, "INSERT INTO person VALUES('Петр')");

以下行仅在字符串

时成功
mysql_query(conn, "SET NAMES utf8");

评论。结果如下:

+------+
| name |
+------+
| Ïåòð |
+------+

enter image description here

但如果该行

mysql_query(conn, "SET NAMES utf8");

没有评论。一开始就会出现错误,帖子(图片)说。

下一步...

设置mysql

mysql> show variables like 'char%';
+--------------------------+-----------------------------------+
| Variable_name            | Value                             |
+--------------------------+-----------------------------------+
| character_set_client     | utf8                              |
| character_set_connection | utf8                              |
| character_set_database   | utf8                              |
| character_set_filesystem | binary                            |
| character_set_results    | utf8                              |
| character_set_server     | utf8                              |
| character_set_system     | utf8                              |
| character_sets_dir       | C:\Database\mysql\share\charsets\ |
+--------------------------+-----------------------------------+
8 rows in set, 5 warnings (0.00 sec)

mysql> show variables like '%colla%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set, 5 warnings (0.00 sec)

mysql>


mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.11, for Win32 (AMD64)

Connection id:          12
Current database:
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.7.11 MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
Uptime:                 14 min 22 sec

Threads: 1  Questions: 70  Slow queries: 0  Opens: 117  Flush tables: 1  Open ta
bles: 110  Queries per second avg: 0.081
--------------

mysql>

我尝试使用utf8mb4,但它出现了同样的问题。 我不知所措。 cp1251但工作正常,但我需要它utf8。 我在这个资源上看到很多帖子。 我没有找到他的问题的解决方案。

请帮忙。

PS。源代码编码utf8

0 个答案:

没有答案