Linux下用OTL操做MySql(包含本身封裝的類庫及演示樣例代碼下載)

版權聲明:本文爲博主原創文章,未經博主贊成不得轉載。 https://blog.csdn.net/ClamReason/article/details/23971805
首先重點推薦介紹otl介紹及使用方法的文章:http://blog.csdn.net/rain_qingtian/article/details/12749177
(1)首先安裝MySql數據庫服務:

下載:http://pan.baidu.com/s/1i3rCnQHhtml

安裝步驟:http://write.blog.csdn.net/postedit/23966241java

(2)安裝navicat數據庫client:

下載:http://pan.baidu.com/s/1i3kMOy5mysql

安裝步驟:傻瓜安裝,選擇字符集的時候本身依據狀況選擇gbk2312,或者utf-8(通常涉及到網絡傳輸或者跨平臺,比方和java項目公用數據。會選擇utf-8)ios

(3)安裝odbc數據庫鏈接驅動:

下載:http://pan.baidu.com/s/1sjPicjFsql

安裝: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項目:post

包括頭文件:otl4.h fetch

下載: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,端口等信息,僅僅要你提供username和password就可以訪問了
		//見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 statementdb // connect object); 

相關文章
相關標籤/搜索