mysqlc++数据库
http://www.cnblogs.com/joeblackzqq/p/4332945.html我按照上面这个链接的教程,用了第二种Connector C++的方法,配置好boost的路径和mysql的路径后,就修改了一下范例的代码:
#include <iostream>#include <map>#include <string>#include <memory>#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;
准备工作都做好了,然后就开始执行:
问题来了:



立即学习“C++免费学习笔记(深入)”;

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号