Linux下用OTL操作MySql(包括自己封装的类库及示例代码下载)

首先重点推荐介绍otl介绍及用法的文章:http://blog.csdn.net/rain_qingtian/article/details/12749177
(1)首先安装MySql数据库服务:

下载:http://pan.baidu.com/s/1i3rCnQH

安装步骤:http://write.blog.csdn.net/postedit/23966241

(2)安装navicat数据库客户端:

下载:http://pan.baidu.com/s/1i3kMOy5

安装步骤:傻瓜安装,选择字符集的时候自己根据情况选择gbk2312,或者utf-8(一般涉及到网络传输或者跨平台,比如和java项目公用数据,会选择utf-8)

(3)安装odbc数据库连接驱动:

下载:http://pan.baidu.com/s/1sjPicjF

安装:http://jingyan.baidu.com/article/8065f87f38b31423312498e4.html 注意这里的安装介绍里面的:打开数据源:开始->设置->控制面板->“管理工具”找到“数据源”

(4)使用otl操作mysql

(4.1)最新封装C++操作otl类库及示例代码下载(可直接运行):http://pan.baidu.com/s/1i31bZUX

(4.2)下面的示例更加简单,由于没有封装otl,只是使用了全局的otl_connect来实现的,方便入门

创建VS项目:

包含头文件:otl4.h 

下载:http://pan.baidu.com/s/1c0tK1jE


包含源文件:

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC // CompileOTL 4.0/ODBC
// Thefollowing #define is required with MyODBC 5.1 and higher
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
#define OTL_UNICODE // CompileOTL with Unicode
#include "otlv4.h"// include the OTL 4.0 header file

otl_connect db; // connect object

void insert()
	// insert rowsinto table
{
	otl_stream o(1, //buffer size should be == 1 always on INSERT.
		"insert into test_tab values(:f1<int>,:f2<char[5]>)",
		// SQLstatement, char[5] means 5 2-byte
		// Unicodecharatcters including a null
		// terminator
		db // connectobject
		);

	unsigned short tmp[32]; // Nullterminated Unicode character array.

	for(int i=1;i<=100;++i){
		o<<i;
		tmp[0]=1111; //Unicode character (decimal code of 1111)
		tmp[1]=2222; //Unicode character (decimal code of 2222)
		tmp[2]=3333; //Unicode chracater (decimal code of 3333)
		tmp[3]=4444; //Unicode chracater (decimal code of 4444)
		tmp[4]=0; //Unicode null terminator
		o<<(unsigned char*)tmp;
		// overloadedoperator<<(const unsigned char*) in the case of Unicode
		// OTL acceptsa pointer to a Unicode character array.
		//operator<<(const unsigned short*) wasn't overloaded
		// in order toavoid ambiguity in C++ type casting.
	}

}

void select()
{
	otl_stream i(50, //buffer size
		" select* from test_tab "
		"where f1>= :f11<int> "
		"  and f1 <= :f12<int>*2 ",
		// SELECTstatement
		db // connectobject
		);
	// create selectstream

	int f1;
	unsigned short f2[32];

	i<<8<<8; // assigning :f11 = 8, f12 = 8
	// SELECTautomatically executes when all input variables are
	// assigned. Firstportion of output rows is fetched to the buffer

	while(!i.eof()){// while not end-of-data
		i>>f1;
		i>>(unsigned char*)f2;
		// overloaded operator>>(unsignedchar*) in the case of Unicode
		// OTL acceptsa pointer to a Unicode chracter array.
		//operator>>(unsigned short*) wasn't overloaded
		// in order toavoid ambiguity in C++ type casting.
		cout<<"f1="<<f1<<", f2=";
		for(int j=0;f2[j]!=0;++j)
			cout<<""<<f2[j];
		cout<<endl;
	}

	i<<4<<4; // assigning :f11 = 4, :f12 = 4
	// SELECTautomatically executes when all input variables are
	// assigned. Firstportion of output rows is fetched to the buffer

	while(!i.eof()){// while not end-of-data
		i>>f1>>(unsigned char*)f2;
		cout<<"f1="<<f1<<", f2=";
		for(int j=0;f2[j]!=0;++j)
			cout<<""<<f2[j];
		cout<<endl;
	}

}

int main()
{
	otl_connect::otl_initialize(); // initialize the database API environment
	try{
		// connect to the database user/psw/dsn,这里的dsn是odbc创建数据源的时候设置的,
		//注意dsn是odbc连接的名字,不是数据库的名字,otl是通过odbc的名字找到数据库的,
		//而这个名字对于的配置里面
		//已经包含了IP,端口等信息,只要你提供用户名和密码就可以访问了
		//见http://jingyan.baidu.com/article/8065f87f38b31423312498e4.html
		db.rlogon("root/123456@local_connect");

		otl_cursor::direct_exec
			(
			db,
			"drop table test_tab",
			otl_exception::disabled // disable OTL exceptions
			); // droptable

		otl_cursor::direct_exec
			(
			db,
			"create table test_tab(f1 int, f2 varchar(11))"
			); // create table

		insert(); //insert records into table
		select(); //select records from table

	}

	catch(otl_exception&p){ // intercept OTL exceptions
		cerr<<p.msg<<endl; // print out error message
		cerr<<p.stm_text<<endl; // print out SQL that caused the error
		cerr<<p.var_info<<endl; // print out the variable that caused the error
	}

	db.logoff(); //disconnect from the database


	getchar();

	return 0;

}

输出:

f1=8, f2=1111222233334444
f1=9, f2=1111222233334444
f1=10, f2=1111222233334444
f1=11, f2=1111222233334444
f1=12, f2=1111222233334444
f1=13, f2=1111222233334444
f1=14, f2=1111222233334444
f1=15, f2=1111222233334444
f1=16, f2=1111222233334444
f1=4, f2=1111222233334444
f1=5, f2=1111222233334444
f1=6, f2=1111222233334444
f1=7, f2=1111222233334444
f1=8, f2=1111222233334444

较好的介绍例子地址:http://www.cnblogs.com/skyme/archive/2010/11/08/1871509.html

语法总结:

连接初始化:otl_connect::otl_initialize();

连接对象:otl_connect db; // connect object

连接数据库:  db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC

连接数据库一般放在try子句中:

 try{
  db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC
 }
 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.sqlstate<<endl; // print out SQLSTATE message
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }
 db.logoff(); // disconnect from ODBC

数据库操作:在数据库的连接和断开之间执行,往往同一个连接内部进行多个数据库操作

db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC

otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions

); // droptable


表的创建

string sql ="create table test_tab(f1 int, f2 varchar(11))";
//create table person_tab(age int, student_name char(30))

otl_cursor::direct_exec
(
db,
sql.c_str();
); // create table

表的删除

otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // droptable

插入

 otl_stream o(50, // buffer size
              "insert into test_tab "
              "values(:f1<int>,:f2<char[31]>,:f3<timestamp>)", 
                 // SQL statement
              db // connect object
             );

  o<<i<<f2<<f3;

查询

otl_stream i(50, // buffer size
"select * from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",
// SELECT statement
db // connect object
); 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是OTL和Soci链接MySQL数据的简单示例,供您参考。 OTL链接MySQL数据示例代码: ```c++ #include <iostream> #include <string> #include <vector> #include <otlv4.h> int main() { try { std::string db_user = "your_user_name"; std::string db_password = "your_password"; std::string db_name = "your_database_name"; std::string db_host = "your_host_name"; int db_port = 3306; otl_connect db; db.rlogon("user=" + db_user + ";password=" + db_password + ";database=" + db_name + ";host=" + db_host + ";port=" + std::to_string(db_port)); otl_stream os; os.open(1, "SELECT * FROM your_table", db); std::vector<std::string> result; while (!os.eof()) { std::string value; os >> value; result.push_back(value); } os.close(); db.logoff(); for (auto& value : result) { std::cout << value << std::endl; } } catch (otl_exception& e) { std::cerr << e.msg << std::endl; } return 0; } ``` Soci链接MySQL数据示例代码: ```c++ #include <iostream> #include <string> #include <vector> #include <soci/soci.h> #include <soci/mysql/soci-mysql.h> int main() { try { std::string db_user = "your_user_name"; std::string db_password = "your_password"; std::string db_name = "your_database_name"; std::string db_host = "your_host_name"; int db_port = 3306; soci::session db(soci::mysql, "user=" + db_user + " password=" + db_password + " dbname=" + db_name + " host=" + db_host + " port=" + std::to_string(db_port)); std::vector<std::string> result; soci::rowset<soci::row> rs = (db.prepare << "SELECT * FROM your_table"); for (auto it = rs.begin(); it != rs.end(); ++it) { soci::row const& row = *it; std::string value; row.get<std::string>(0, value); result.push_back(value); } for (auto& value : result) { std::cout << value << std::endl; } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; } ``` 这两个都有简单易用的API,选择哪一个更好需要根据个人需求和项目要求来决定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值