#include <iostream> #include "Poco/Data/MySQL/Connector.h" #include "Poco/Exception.h" #include "Poco/Data/Session.h" #include "Poco/Data/Statement.h" using namespace std; using namespace Poco::Data; using Poco::Data::Session; int main() { MySQL::Connector::registerConnector(); string user = "root"; string password = "111"; string host = "localhost"; string db = "test"; string connection_str = "user=" + user + ";password=" + password + ";host=" + host + ";db=" + db + ";auto-reconnect=true"; string collation_str = "set collation_connection=utf8_general_ci, " "character_set_results=utf8, " "character_set_connection=utf8, " "character_set_client=utf8"; Session ses("MySQL", connection_str); string out; try { ses << "SELECT name FROM `test` LIMIT 1", into(out), now; } catch (Poco::Exception &e) { cout << e.displayText() << endl; } cout << "without collation: " << out << endl; try { ses << collation_str, now; ses << "SELECT name FROM `test` LIMIT 1", into(out), now; } catch (Poco::Exception &e) { cout << e.displayText() << endl; } cout << "with collation: " << out << endl; return 0; }output:
without collation: ???? with collation: тест
SQL:
-- phpMyAdmin SQL Dump -- -------------------------------------------------------- -- -- Структура таблицы `test` -- CREATE TABLE IF NOT EXISTS `test` ( `id` int(11) NOT NULL, `name` varchar(250) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Дамп данных таблицы `test` -- INSERT INTO `test` (`id`, `name`) VALUES (1, 'тест');задать глобальное значение utf8 в /etc/my.cnf:
[mysqld] # Settings user and group are ignored when systemd is used. # If you need to run mysqld under different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [client] default-character-set=utf8 [mysqld] character_set_server=utf8 collation_server=utf8_general_ci
Комментариев нет:
Отправить комментарий