MySQL Forums
Forum List  »  Connector/C++

ResultSet->getString() code
Posted by: tai a
Date: January 09, 2019 10:17PM

mysql> show variables like '%char%';
+--------------------------+---------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:\YJF\YJFMySQL\mysql\share\charsets\ |
+--------------------------+---------------------------------------+
8 rows in set (0.00 sec)

mysql> use ygx;
Database changed
mysql> show create database ygx;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| ygx | CREATE DATABASE `ygx` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table test;
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`姓名` varchar(20) DEFAULT NULL,
`年龄` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> use ygx;
Database changed
mysql> select * from test;
+------+------+
| 姓名 | 年龄 |
+------+------+
| 张三 | 20 |
+------+------+
1 row in set (0.00 sec)




int main()
{
sql::mysql::MySQL_Driver *driver = NULL;
sql::Connection *conn = NULL;
sql::Statement *stmt = NULL;
sql::ResultSet *res = NULL;
try
{
driver = sql::mysql::get_mysql_driver_instance();
conn = driver->connect("tcp://localhost:3306/ygx", "root", "123456");
cout << "连接成功!" << endl;
}
catch (const std::exception&)
{
cout << "连接失败!" << endl;
goto END;
}
try
{
stmt = conn->createStatement();
stmt->execute("set character set gbk");
/* stmt->execute("set character_set_results= gbk");
stmt->execute("set character_set_client=gbk");
stmt->execute("set character_set_connection= gbk");
*/ res = stmt->executeQuery("select * from test where 姓名='张三'");

}
catch (const std::exception&)
{
cout << "查询失败!" << endl;
goto END;
}
cout << "查询成功!" << endl;
try
{
if (res->next())
{
string str;
// string str1 = string_to_utf8(str);
str = res->getString(1).c_str();
// str1 = utf8_to_string(str);
}
}
catch (const std::exception&)
{
cout << "显示失败!" << endl;
goto END;
}
END:
delete res;
delete stmt;
delete conn;
return 0;
}


str = res->getString(1).c_str();
str=张三

str = res->getString("张三").c_str();
str=20

why???

Options: ReplyQuote


Subject
Views
Written By
Posted
ResultSet->getString() code
901
January 09, 2019 10:17PM
453
January 22, 2019 08:50AM
417
January 30, 2019 01:07AM
435
February 05, 2019 04:19AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.