日期:2011-01-25 22:43:00 来源:本站整理
<b>MySQL 5 C API 拜候数据库例子程序</b>[MySQL防范]
本文“<b>MySQL 5 C API 拜候数据库例子程序</b>[MySQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
/***************************************************************** LQU@7gE0<m
以下是研究 mysql 5.0 得出的后果,描写并利用尺度 c++演示了利用 MySQL [87PH
C API 函数 简单操作数据库的流程; T+E{PA~3
例子程序在 VC6 + windows 2000 上调试通过 cKAusP=I.
*****************************************************************/ A[s<+P"
#include <windows.h> 7a7g'my
#include <iostream> Hkp>}:#!o_
#include <mysql.h> //文件位于 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"<<endl; ~uvN .7RZ
return -1; yQ;{?N,5<E
} 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"<<endl<<mysql_error(&mydata)<<endl; 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"<<endl<<mysql_error(&mydata)<<endl; 6LnFyBK$
return -1; t[`!+ W!
} a3ied;1)I
} EjvU*9H QQ
else s S;mMe m]
{ v$k1"s,
std::cout<<"run parameter error"<<endl; 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<sbT
mysql_close(&mydata); O"$-@c*
std::cout<<"drop table fail"<<endl<<mysql_error(&mydata)<<endl; %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"<<endl S=2V+c' G\
<<mysql_error(&mydata)<<endl; ]]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<<mysql_error(&mydata)<<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); /<CUAGS4
std::cout<<"execute sql syntax fail"<< n8SbdhL
endl<<mysql_error(&mydata)<<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: "<<s_sql.c_str()<<",row count: "<<rowcount<<endl; >^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<<fields->name<<"\t\t"; Fc_&UL"47
} !acyLmP`\
std::cout<<endl; 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<<endl; 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<zn,}b
} 以上是“<b>MySQL 5 C API 拜候数据库例子程序</b>[MySQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
<b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
<b>在 Windows 8 中手动安装语言包</b>
<b>五个常见 PHP数据库问题</b>
Windows中Alt键的12个高效快速的利用本领介绍
<b>MySQL ORDER BY 的实现解析</b>
<b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
<b>Win8系统恢复出来经典的开始菜单的办法</b>
<b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
<b>Windows 7系统下无线网卡安装</b>
<b>为什么 Linux不需求碎片整理</b>
<b>Windows 8中删除账户的几种办法(图)</b>
<b>教你如安在win7下配置路由器</b>
以下是研究 mysql 5.0 得出的后果,描写并利用尺度 c++演示了利用 MySQL [87PH
C API 函数 简单操作数据库的流程; T+E{PA~3
例子程序在 VC6 + windows 2000 上调试通过 cKAusP=I.
*****************************************************************/ A[s<+P"
#include <windows.h> 7a7g'my
#include <iostream> Hkp>}:#!o_
#include <mysql.h> //文件位于 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"<<endl; ~uvN .7RZ
return -1; yQ;{?N,5<E
} 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"<<endl<<mysql_error(&mydata)<<endl; 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"<<endl<<mysql_error(&mydata)<<endl; 6LnFyBK$
return -1; t[`!+ W!
} a3ied;1)I
} EjvU*9H QQ
else s S;mMe m]
{ v$k1"s,
std::cout<<"run parameter error"<<endl; 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<sbT
mysql_close(&mydata); O"$-@c*
std::cout<<"drop table fail"<<endl<<mysql_error(&mydata)<<endl; %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"<<endl S=2V+c' G\
<<mysql_error(&mydata)<<endl; ]]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<<mysql_error(&mydata)<<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); /<CUAGS4
std::cout<<"execute sql syntax fail"<< n8SbdhL
endl<<mysql_error(&mydata)<<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: "<<s_sql.c_str()<<",row count: "<<rowcount<<endl; >^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<<fields->name<<"\t\t"; Fc_&UL"47
} !acyLmP`\
std::cout<<endl; 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<<endl; 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<zn,}b
} 以上是“<b>MySQL 5 C API 拜候数据库例子程序</b>[MySQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论