Home  >  Article  >  Database  >  MySQL 5 C API 访问数据库例子程序_MySQL

MySQL 5 C API 访问数据库例子程序_MySQL

WBOY
WBOYOriginal
2016-06-01 14:03:231134browse

/***************************************************************** LQU@7gE0 
以下是研究 mysql 5.0 得出的结果,描述并使用标准 c++演示了使用 MySQL [87PH  
C API 函数 简单操作数据库的流程; T+E{PA~3  
例子程序在 VC6 + windows 2000 上调试通过 cKAusP=I.  
*****************************************************************/ A[s"  
#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 ~uvN .7RZ  
return -1; yQ;{?N,5 
} KZKHXM?  
Nd'HTI  
//连接数据库 O M^>G"!  
if(argc == 1) q|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 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 6LnFyBK$  
  return -1; t[`!+ W!  
} a3ied;1)I  
} EjvU*9H QQ  
else s S;mMe m]  
{ v$k1"s,  
std::cout 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 %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 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 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 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"; YpXAp  
if(mysql_query(&mydata,s_sql.c_str()) != 0) e&Nm%#9Z;  
{ +n1)a7j  
//执行SQL语句出错 =PaE\  
mysql_close(&mydata); /

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn