mysql connector getstring出错_c++-C++连接mysql数据库时,用getString()方法总是出错

http://www.cnblogs.com/joeblackzqq/p/4332945.html

我按照上面这个链接的教程,用了第二种Connector C++的方法,配置好boost的路径和mysql的路径后,就修改了一下范例的代码:#include #include #include #include #include "mysql_driver.h"#include "mysql_connection.h"#include "cppconn/driver.h"#include "cppconn/statement.h"#include "cppconn/prepared_statement.h"#include "cppconn/metadata.h"#include "cppconn/exception.h"using namespace std;using namespace sql;int main(){ sql::mysql::MySQL_Driver *driver = 0; sql::Connection *conn = 0; try { driver = sql::mysql::get_mysql_driver_instance(); conn = driver->connect("tcp://localhost:3306/tree", "root", "123"); cout << "连接成功" << endl; } catch (...) { cout << "连接失败" << endl; } sql::Statement* stat = conn->createStatement(); stat->execute("set names 'gbk'"); ResultSet *res; res = stat->executeQuery("SELECT * FROM testuser"); while (res->next()) { cout << "Id :" << res->getInt("id") << endl; cout << "Name :" << res->getString("name") << endl; //cout << "Address :" << res->getString("address") << endl; } if (conn != 0) { delete conn; } system("pause");}

为此我创建了一个数据库tree和表testuser:create database tree;use tree;create table testuser(id INT(4) PRIMARY KEY,name CHAR(25),address varchar(45));desc testuser;insert into testuser values(1,'Tom','China Beijing'),(2,'Amy','America NewYork');select*from testuser;

准备工作都做好了,然后就开始执行:

问题来了:

1455080707_998865.png

1455080737_523258.png

1455080768_480387.png

1455080792_779487.png

1455080844_811925.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 可以通过使用 MySQL Connector/C++连接和操作 MySQL 数据库MySQL Connector/C++MySQL 官方提供的 C++ 驱动程序,可以在 Qt 项目中使用。 要在 Qt 项目中使用 MySQL Connector/C++,首先需要下载并安装 MySQL Connector/C++。然后,在 Qt 项目中添加 MySQL Connector/C++ 的头文件和库文件。 以下是一些步骤来使用 MySQL Connector/C++: 1. 下载和安装 MySQL Connector/C++。你可以从 MySQL 官方网站下载适用于你的操作系统的安装程序。 2. 在 Qt 项目中添加 MySQL Connector/C++ 的头文件路径。在.pro 文件中添加以下行: ``` INCLUDEPATH += /path/to/mysql-connector-c++/include ``` 3. 在 Qt 项目中添加 MySQL Connector/C++ 的库文件路径。在.pro 文件中添加以下行: ``` LIBS += -L/path/to/mysql-connector-c++/lib -lmysqlcppconn ``` 4. 在代码中包含 MySQL Connector/C++ 的头文件,并使用其提供的类和方法进行数据库连接和操作。例如: ```cpp #include <mysql_driver.h> #include <mysql_connection.h> // 创建连接 sql::mysql::MySQL_Driver *driver; sql::Connection *con; driver = sql::mysql::get_mysql_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); // 执行查询 sql::Statement *stmt; sql::ResultSet *res; stmt = con->createStatement(); res = stmt->executeQuery("SELECT * FROM table"); // 处理结果 while (res->next()) { // 从结果集中获取数据 int id = res->getInt("id"); QString name = QString::fromStdString(res->getString("name")); // 处理数据 // ... } // 清理资源 delete res; delete stmt; delete con; ``` 这样,你就可以在 Qt 项目中使用 MySQL Connector/C++连接和操作 MySQL 数据库了。记得替换代码中的用户名、密码、表名等信息为你实际的数据库信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值