多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 代码示例 ```c++ // MysqlConnector.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> #include <mysql_driver.h> #include <mysql_connection.h> #include <mysql_driver.h> #include <driver.h> #include <connection.h> #include <statement.h> #include <prepared_statement.h> #include <resultset.h> #include <metadata.h> #include <resultset_metadata.h> #include <exception.h> #include <warning.h> using namespace sql; using namespace std; #pragma comment(lib,"mysqlcppconn.lib") #pragma comment(lib,"mysqlcppconn-static.lib") /*void RunnConnectionMysql() { mysql::MySQL_Driver *driver; Connection *conn; Statement *state; ResultSet *res; sql::PreparedStatement *prep_stmt; Savepoint *savept; driver = sql::mysql::get_mysql_driver_instance(); conn = driver->connect("127.0.0.1:3306", "root", "www.upsoft01.com"); conn->setAutoCommit(0); state = conn->createStatement(); // state->execute("use test"); conn->setSchema("test"); state->execute("create table if not exists tt (id int not null auto_increment, name varchar(20) not null, age int, primary key(id))"); state->execute("delete from tt"); state->execute("drop table tt"); res = state->executeQuery("select * from user"); ResultSetMetaData * metaData = res->getMetaData(); int rows = (int)res->rowsCount(); int cols = (int)metaData->getColumnCount(); cout << "the cols num:" << cols << endl; cout << "the rows num:" << rows << endl; while(res->next()) { string name = res->getString("name"); int sex = res->getInt("sex"); cout << name << " " <<sex <<endl; } //print column name and column type for(int i=0;i<cols; ++i) { cout << metaData->getColumnLabel(i+1) << " " << metaData->getColumnTypeName(i+1) << endl; } //print table name and database name cout << metaData->getTableName(1) << " : " << metaData->getSchemaName(1) << endl; //change the data of table prep_stmt = conn->prepareStatement("insert into user(name, sex, password) values(?, ?, ?)"); prep_stmt->setString(1, "lala"); prep_stmt->setInt(2, 24); prep_stmt->setString(3, "lala"); int updatecount = prep_stmt->executeUpdate(); //set save point savept = conn->setSavepoint("savept1"); prep_stmt->setString(1, "hehe"); prep_stmt->setInt(2, 2); prep_stmt->setString(3, "lala"); updatecount = prep_stmt->executeUpdate(); conn->rollback(savept); conn->releaseSavepoint(savept); conn->commit(); delete res; delete state; delete prep_stmt; conn->close(); delete conn; }*/ /** * 判断查询结果集中是否存在某列 * @param rs 查询结果集 * @param columnName 列名 * @return true 存在; false 不存咋 */ int main() { //RunnConnectionMysql(); //getchar(); sql::mysql::MySQL_Driver *driver; sql::Connection *conn; sql::Statement *state; sql::ResultSet *result; driver = sql::mysql::get_mysql_driver_instance(); conn = driver->connect("192.168.0.211", "root", "www.upsoft01.com"); if (!conn) { cout << "failed connect" << endl;return 0; } state = conn->createStatement(); state->execute("set names 'gbk'"); state->execute("use antdbms_wenzhou"); result = state->executeQuery("select user_id,user_name from hs_user"); //// 输出查询 while (result->next()!=NULL) { string s = "user_name"; string ret = result-> getString(s); } if (conn != 0) { delete conn; } //Sleep(10000); system("pause"); return 0; }