c++ - 使用 MysqlConnector/C 还是 MysqlConnector/C++?

标签 c++ mysql sql c visual-studio

我使用 Visual Studio 2012。我有控制台应用程序。我应该连接到mysql服务器,我使用mysql连接器/C++,但是当我从表中读取时,例如,如果我应该得到“word”,我会得到四个未知符号,“word”,然后是许多未知符号(也有字)并以 fatal error 结束。什么是问题?我应该使用连接器 C 吗?

这是我的代码`

sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("localhost", "root", "pass");
stmt = con->createStatement();
stmt->execute("USE mail_members");
sql::ResultSet* res = stmt->executeQuery("SELECT id FROM messages");
int k = 0;
res->next();
std::cout << res->getString("id").asStdString();
delete con;
delete stmt;

有人用Mysql Connector.c++吗?

附注这是我的消息表`

Field    Type    NULL     Key     Default  |  Extra
id       int(11) Yes              NULL
message  text    YES              NULL

我在其他应用程序中使用 MysqlConnector/C# 并且它工作正常,这可能不起作用,因为我的应用程序是 consiole(带有 stdafx 文件)?

最佳答案

您的代码是否抛出任何异常?

我将字段名称放入 std::string 中,然后传递给结果集方法:

const std::string   name = fri.get_field_name();
unsigned int        result_value = m_resultset.getUInt(name);

在我的表中,记录 ID 字段是整数而不是字符串。

+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Title    | varchar(64)      | NO   | UNI | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.08 sec)

你的ID字段是字符串吗?

关于c++ - 使用 MysqlConnector/C 还是 MysqlConnector/C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20058583/

相关文章:

连接到复制的 mysql 数据库的 Java 应用程序

mysql - SERIALIZED CONTEXT 数据截断问题 [SPRING BATCH]

mysql - 从 2 个表中选择总数量

php - MySQL SELECT 语句只搜索表中的 3 个字段

sql - Real * Bigint 不返回正确的值

c++ - 带有 LinkedList 的迭代器类?

c++ - 是否可以从覆盖(非事件)窗口获取 pixelcolor(x,y)?

c++ - Boost 线程中断在跨越 DLL 边界时不起作用

c++ - 类如何比结构更安全?

sql - SQL中选择该ID存在于另一个表上的数据