MySQL5CAPI访问数据库例子程序_MySQL
时间:2021-07-01 10:21:17
帮助过:20人阅读
/***************************************************************** LQU@7gE0
以下是研究 mysql 5.0 得出的结果,描述并使用标准 c++演示了使用 MySQL [87PH
C API 函数 简单操作数据库的流程; T+E{PA~3
例子程序在 VC6 + windows 2000 上调试通过 cKAusP=I.
*****************************************************************/ A[s<+P"
#include 7a7g'my
#include Hkp>}:#!o_
#include //文件位于 MySQL 提供的 C API 目录中 F iF7hj4ld
using namespace std; UuWny}>
q5x t.kuY
// linux 等系统中请加入 -lmysql qwe yX)
#pragma comment( lib, "libmysql.lib") >OY}SzU
if@1PPG5
07)7f[
/*****************************************************************/ DW,r !uI(
///name : main &H[l 8>
//function : 主测试函数 $S~+)`-C
//access : private Zhf}SMRJ}
//para : 2>3bqd|
// 1. : int argc NWg !k!
// : 系统参数个数 ` C| #e
// 2. : char * argv[] =pF Vw@ 8
// : 参数数值 ICD# RY &
//return : 返回给 startup 函数的退出参数 <)c4]PW
//author : hzh nR?k*Ez
//date : 2006-06-24 O0"U=A{O\
/*****************************************************************/ ZB ^R\2 J
int main( int argc, char * argv[] ) cr,ZH:Z
{ f*Y353gB
MYSQL mydata; 7 (l:l
Fu Ez-h
//初始化数据结构 7+ {w5\4A
if(mysql_init(&mydata) == NULL) G4G?;0~\4
{ LpZe b`Aj
std::cout<<"init mysql data stauct fail"< ~uvN .7RZ
return -1; yQ;{?N,5
} KZKHXM?
Nd'HTI
//连接数据库 O M^>G"!
if(argc == 1) <-~fpq|4x
{ nYa5f-<
if(NULL == mysql_real_connect(&mydata,"127.0.0.1","root","mysql5", !_cP ajA
"hzhdb",MYSQL_PORT,NULL,0)) fS(|z37^
{ T6|KX"NNlO
std::cout<<"connect database fail"< gAb &Z
return -1; TZv[b&/
} NKkdL
}else if(argc == 5) p-Vce#^no
{ ;8kj1a-i V
if(NULL == mysql_real_connect(&mydata,argv[1], +\+ySQt
argv[2],argv[3],argv[4],MYSQL_PORT,NULL,0)) e1Dl7^]9<
{ wP`$n x:
std::cout<<"connect database fail"< 6LnFyBK$
return -1; t[`!+ W!
} a3ied;1)I
} EjvU*9H QQ
else s S;mMe m]
{ v$k1"s,
std::cout<<"run parameter error"< dChiz~%[
return -1; M>!*6) a+
} L7Y 8D)
-npl(^K
std::string s_sql = "drop table hzhtest"; DRA(+=%
if(mysql_query(&mydata,s_sql.c_str()) != 0) 0 uf'gbjf
{ xr%3!l?
//删除表失败 pG{:RU
mysql_close(&mydata); O"$-@c*
std::cout<<"drop table fail"< %UXxP(g
} n)!=%*7
ks{qrYZK
//创建数据表,字段 myid 设置了自增列属性 a|20-c1)c
s_sql = "create table hzhtest("; p1xJ) N8Z{
s_sql += "myid integer not null auto_increment,"; 8tv6?}t|]
s_sql += "mytime datetime null,myname varchar(30),"; >fh[Z: tf
s_sql += " primary key(myid))"; #i=R6
if(mysql_query(&mydata,s_sql.c_str()) != 0) +jP}<R>/
{ 3 l_ yx}
//创建表失败 _SrVC$j
mysql_close(&mydata); ^Cd/ Ox|u
std::cout<<"create table fail"< S=2V+c' G\
< ]]k%g@W
return -1; mh`8K[H
} K d OY 06<
SFSs)rD
//向表中插入数据 /-Qu,T)
for(int k = 1; k < 30; ++k) w7h)Ke@ n
{ OU#H-Xq
s_sql = "insert into hzhtest(mytime,myname) values"; aShy69k:_V
s_sql += "('2006-06-"; R9$4woWi
char buff[20]; )NNs7uN/
memset(buff,0,sizeof(buff)); xI"'3) q
itoa(k,buff,10); F :YQt_
s_sql += buff; J5UJ)Gdb
@.qj$e{e
s_sql += " "; K(Y4MVI{U;
i.uji VvK
int i = k % 3; (8V5kf }
memset(buff,0,sizeof(buff)); -k:{H;SK
itoa(i,buff,10); clErn0M
s_sql += buff; Sb"{B08q1
s_sql += ":01:01'"; EM*)nW^=j
o!.bLkO)
if(i == 0) ov .\wNe
{ =8)\z$b
s_sql += ",NULL"; |YTM`8\}LG
} \>m*FA
else C.U9(DfA/5
{ QNp5qZt+
s_sql += ",'黄志辉"; s!DxV
s_sql += buff; IPhbq.
s_sql += "'"; 6:IWmt$S
} O@Q^j+#
s_sql += ")"; qx#z%:Ep
]LbE\q),
if(mysql_query(&mydata,s_sql.c_str()) != 0) +*$c|as
{ ]}3Xyw
//执行SQL语句出错 c>M*|(,
std::cout<<"execute insert syntax fail"<< 2gq+mqEm
endl< @;u}.)i%
mysql_close(&mydata) ; kuh.c#
return -1; \Z c/-s\
} ceoSPr`#
} w??NGBL
l AU>JDX
//查询数据并显示 98}A pK
s_sql = "select myid,mytime,myname from hzhtest"; YpXA<p
if(mysql_query(&mydata,s_sql.c_str()) != 0) e&Nm%#9Z;
{ +n1)a7j
//执行SQL语句出错 =PaE\
mysql_close(&mydata); /
std::cout<<"execute sql syntax fail"<< n8SbdhL
endl< 2 Z+,7|qA>
return -1; @t^&@:C
} Y3I@}f^5
2QVwmITn
F+uY1;\XOS
MYSQL_RES *result = mysql_store_result(&mydata); ;Oq#Sh
mJ8z!D-kr
//取得查询结果 I;!@ `0
int rowcount = mysql_num_rows(result); MbBoxy
//取得有效记录数 )U#Q 1{O
std::cout<<"exec sql: "< >^h!_E99
H0?J'PV\0e
MYSQL_FIELD *fields = NULL; xp NRV= U/
//取得各字段名 j/ ds
for(int i = 0; fields = mysql_fetch_field(result);++i) {`28> So
{ g>-ZXEq
std::cout<name<<"\t\t"; Fc_&UL"47
} !acyLmP`\
std::cout< a ?m9x
7m`(MVXg
,r8Ix"
//依次读取各条记录 dH C%4IW"R
MYSQL_ROW currrow = NULL; e!a:'W7LM
while((currrow = mysql_fetch_row(result)) != NULL) EG F4X4srl
{ +n?ETS
//读行的记录 jx9mC9]e[
for(int i = 0; i < mysql_num_fields(result); ++i) rCHl
{ tpW.ne&ZR
std::cout<<(currrow ? currrow : "NULL")<<"\t"; K E;j[ #{
} q6yF>$;
std::cout< t1{@TN@
} 6NK[d
@Ulr0ifw
mysql_free_result(result) ; UbY'F!2
mysql_close(&mydata); Q"4;!MmE'
vpgtt(4 X
system("pause"); Oy5v.Z$J7
k/N53$
return 1; _9
}