ylliu / -

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C++ 调用msyql的ResultSet

ylliu opened this issue · comments

commented

本机测试发现要编译64位,因为安装的数据库是64位的,所以工程要编译成64位的
当前电脑的版本mysqlcppconn.dll是release版本的,要编译成release版本
在工程编译的时候,要把mysqlcppconn.dll放在源码的路径下,编译时需要动态导入
使用RESULTRET 需要添加boost的路径
https://www.cnblogs.com/joeblackzqq/p/4332945.html
https://blog.csdn.net/above_my_point/article/details/78934163

commented

#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/patrolcar", "root", "123456");
	cout << "连接成功" << endl;
}
catch (...)
{
	cout << "连接失败" << endl;
}
sql::Statement* stat = conn->createStatement();
stat->execute("set names 'gbk'");
ResultSet *res;
res = stat->executeQuery("SELECT * FROM observationinfo");
while (res->next())
{
	cout << "target_id:" << res->getString("target_id") << endl; //数据库的列名获取
	cout << "device_id:" << res->getString("device_id") << endl;
	cout << "target_id:" << res->getString(1) << endl;			 //按数据库的列号获取
	cout << "device_id:" << res->getString(2) << endl;
	cout << "excute_status:" << res->getInt(4) << endl;			//存储的是int类型就调用int的方法,具体可以看定义
}
if (conn != 0)
{
	delete conn;
}
system("pause");

}