c++连接mysql编程(mysq-connector/c++)

本文环境为linux

下载c++链接mysql的头文件及动态库:http://mirrors.sohu.com/mysql/Connector-C%2b%2b/

mysql-connector-c++-1.1.6-linux-glibc2.5-x86-32

注意:由于此链接库需要boost,所以还需要下载安装boost库

解压缩到某个文件下,解压目录如下:

.
├── include
│   ├── cppconn
│   │   ├── build_config.h
│   │   ├── config.h
│   │   ├── connection.h
│   │   ├── datatype.h
│   │   ├── driver.h
│   │   ├── exception.h
│   │   ├── metadata.h
│   │   ├── parameter_metadata.h
│   │   ├── prepared_statement.h
│   │   ├── resultset.h
│   │   ├── resultset_metadata.h
│   │   ├── sqlstring.h
│   │   ├── statement.h
│   │   ├── variant.h
│   │   ├── version_info.h
│   │   └── warning.h
│   ├── mysql_connection.h
│   ├── mysql_driver.h
│   └── mysql_error.h
├── lib
│   ├── libmysqlcppconn.so -> libmysqlcppconn.so.7
│   ├── libmysqlcppconn.so.7 -> libmysqlcppconn.so.7.1.1.6
│   ├── libmysqlcppconn.so.7.1.1.6
│   └── libmysqlcppconn-static.a
├── Makefile

此时需要将动态库:libmysqlcppconn.so.7 拷贝到 /usr/local/lib下,并创建链接到/usr/lib:ln -s  /usr/local/lib/libmysqlcppconn.so.7 /usr/lib,


编程实例:

#include "mysql_connection.h"
#include "mysql_driver.h"
#include "cppconn/statement.h"
#include "cppconn/resultset.h"
#include "cppconn/connection.h"
#include <cppconn/prepared_statement.h>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace sql;
using namespace sql::mysql;

void RunnConnectionMysql()
{
	mysql::MySQL_Driver *driver;
	Connection *conn;
	Statement *state;
	ResultSet *res;
	sql::PreparedStatement *prep_stmt;
	Savepoint *savept;
	
	driver = sql::mysql::get_driver_instance();
	conn = driver->connect("127.0.0.1:3306", "root", "root");
	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;
}

int main(int argc, char *argv[])
{
    RunnConnectionMysql();
	getchar();
    return 0;
}



编译:

g++ -g -o test mysqltest.cpp  -I/home/laijia/database/include -I/home/laijia/database/include/cppconn -I/usr/include -L/home/laijia/database/lib -L/usr/local/lib -lmysqlcppconn -lmysqlcppconn-static

必须添加-lmysqlcppconn -lmysqlcppconn-static,否则会报错误




你可能感兴趣的:(C++,mysql)