用C语言操作MySQL数据库,进行连接、插入、修改、删除等操作

news/2024/5/18 15:47:43 标签: mysql, 数据库, 语言, c, query, bbs
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

http://www.huanbaocity.com/index.php?action=plugin&v=wave&tpl=union&ac=viewgrouppost&gid=76&tid=3876

 

很多人用到MySQL来开发一些项目࿰c;有时为了性能࿰c;我们会直接用Cclass="tags" href="/tags/YuYan.html" title=语言>语言来开发相关的模块࿰c;尤其在我们的web应用中࿰c;虽然PHP、JSP等脚本均 提供了MySQL的接口࿰c;但是显然直接使用Cclass="tags" href="/tags/YuYan.html" title=语言>语言具有更好的安全性和性能࿰c;Michael以前用PHP开发的多个项目中就使用了Cclass="tags" href="/tags/YuYan.html" title=语言>语言编写的这类接口࿰c;然 后再编译到php里面࿰c;供php脚本直接使用,这方面的话题就不多说了࿰c;下面主要说一下在Linux下如何用Cclass="tags" href="/tags/YuYan.html" title=语言>语言连接MySQLclass="tags" href="/tags/ShuJuKu.html" title=数据库>数据库࿰c;并且读取里面的 数据返回࿰c;同时如何进行编译。

  这里的大部分代码参考了MySQL发行包里面的.c源文件࿰c;大家也可以去里面找找相关的代码࿰c;下面这段代码实现了连接到本地MySQL服务器上 9tmd_bbs_utf8class="tags" href="/tags/ShuJuKu.html" title=数据库>数据库࿰c;从数据表tbb_user中根据输入的userid取得该用户的用户名并打印输出到终端。

class="hl-surround">
class="hl-main"> class="hl-prepro">#if color: gray;"> color: blue;">defined color: olive;">( color: blue;">_WIN32 color: olive;">) color: gray;"> || color: blue;">defined color: olive;">( color: blue;">_WIN64 color: olive;">) color: gray;">  color: #ffa500;">// 为了支持windows平台上的编译 color: gray;">
class="hl-prepro">#include color: #8b0000;">< color: red;">windows.h color: #8b0000;">> color: gray;">
class="hl-prepro">#endif color: gray;">
class="hl-prepro">#include color: #8b0000;">< color: red;">stdio.h color: #8b0000;">> color: gray;">
class="hl-prepro">#include color: #8b0000;">< color: red;">stdlib.h color: #8b0000;">> color: gray;">
class="hl-prepro">#include color: #8b0000;">" color: red;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql.h color: #8b0000;">" class="hl-prepro">  //我的机器上该文件在/usr/local/include/class="tags" href="/tags/MYSQL.html" title=mysql>mysql下 color: gray;">
 
color: #ffa500;">//定义class="tags" href="/tags/ShuJuKu.html" title=数据库>数据库操作的宏࿰c;也可以不定义留着后面直接写进代码 color: gray;">
class="hl-prepro">#define color: gray;"> color: blue;">SELECT_QUERY color: gray;"> color: #8b0000;">" color: red;">select username from tbb_user where userid = %d color: #8b0000;">" color: gray;">
 
class="hl-types">int color: gray;"> color: blue;">main color: olive;">( class="hl-types">int color: gray;"> color: blue;">argc color: gray;">, class="hl-types">char color: gray;"> ** color: blue;">argv color: olive;">) color: gray;"> color: #ffa500;">//char **argv 相当于 char *argv[] color: gray;">
color: olive;">{ color: gray;">
    
color: blue;">MYSQL color: gray;"> color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql color: gray;">,* color: blue;">sock color: gray;">;    color: #ffa500;">// 定义class="tags" href="/tags/ShuJuKu.html" title=数据库>数据库连接的句柄࿰c;它被用于几乎所有的MySQL函数 color: gray;">
    
color: blue;">MYSQL_RES color: gray;"> * color: blue;">res color: gray;">;        color: #ffa500;">//查询结果集࿰c;结构类型 color: gray;">
    
color: blue;">MYSQL_FIELD color: gray;"> * color: blue;">fd color: gray;"> ;      color: #ffa500;">//包含字段信息的结构 color: gray;">
    
color: blue;">MYSQL_ROW color: gray;"> color: blue;">row color: gray;"> ;        color: #ffa500;">//存放一行查询结果的字符串数组 color: gray;">
    
class="hl-types">char color: gray;">  color: blue;">qbuf color: olive;">[ color: maroon;">160 color: olive;">] color: gray;">;      color: #ffa500;">//存放查询sql语句字符串 color: gray;">
    
    
color: green;">if color: gray;"> color: olive;">( color: blue;">argc color: gray;"> != color: maroon;">2 color: olive;">) color: gray;"> color: olive;">{ color: gray;">  color: #ffa500;">//检 查输入参数 color: gray;">
        
color: blue;">fprintf color: olive;">( color: blue;">stderr color: gray;">, color: #8b0000;">" color: red;">usage : class="tags" href="/tags/MYSQL.html" title=mysql>mysql_select <userid> color: red;">n color: red;">n color: #8b0000;">" color: olive;">) color: gray;">;
        
color: blue;">exit color: olive;">( color: maroon;">1 color: olive;">) color: gray;">;
    
color: olive;">} color: gray;">
    
    
color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_init color: olive;">( color: gray;">& color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql color: olive;">) color: gray;">;
    
color: green;">if color: gray;"> color: olive;">( color: gray;">! color: olive;">( color: blue;">sock color: gray;"> = color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_real_connect color: olive;">( color: gray;">& color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql color: gray;">, color: #8b0000;">" color: red;">localhost color: #8b0000;">" color: gray;">, color: #8b0000;">" color: red;">dbuser color: #8b0000;">" color: gray;">, color: #8b0000;">" color: red;">dbpwd color: #8b0000;">" color: gray;">, color: #8b0000;">" color: red;">9tmd_bbs_utf8 color: #8b0000;">" color: gray;">, color: maroon;">0 color: gray;">, class="hl-prepro">NULL color: gray;">, color: maroon;">0 color: olive;">))) color: gray;"> color: olive;">{ color: gray;">
        
color: blue;">fprintf color: olive;">( color: blue;">stderr color: gray;">, color: #8b0000;">" color: red;">Couldn't connect to engine! color: red;">n%s color: red;">n color: red;">n color: #8b0000;">" color: gray;">, color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_error color: olive;">( color: gray;">& color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql color: olive;">)) color: gray;">;
        
color: blue;">perror color: olive;">( color: #8b0000;">"" color: olive;">) color: gray;">;
        
color: blue;">exit color: olive;">( color: maroon;">1 color: olive;">) color: gray;">;
    
color: olive;">} color: gray;">
    
    
color: blue;">sprintf color: olive;">( color: blue;">qbuf color: gray;">, color: blue;">SELECT_QUERY color: gray;">, color: blue;">atoi color: olive;">( color: blue;">argv color: olive;">[ color: maroon;">1 color: olive;">])) color: gray;">;
    
color: green;">if color: olive;">( color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_query color: olive;">( color: blue;">sock color: gray;">, color: blue;">qbuf color: olive;">)) color: gray;"> color: olive;">{ color: gray;">
        
color: blue;">fprintf color: olive;">( color: blue;">stderr color: gray;">, color: #8b0000;">" color: red;">Query failed (%s) color: red;">n color: #8b0000;">" color: gray;">, color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_error color: olive;">( color: blue;">sock color: olive;">)) color: gray;">;
        
color: blue;">exit color: olive;">( color: maroon;">1 color: olive;">) color: gray;">;
    
color: olive;">} color: gray;">
    
    
color: green;">if color: gray;"> color: olive;">( color: gray;">! color: olive;">( color: blue;">res color: gray;">= color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_store_result color: olive;">( color: blue;">sock color: olive;">))) color: gray;"> color: olive;">{ color: gray;">
        
color: blue;">fprintf color: olive;">( color: blue;">stderr color: gray;">, color: #8b0000;">" color: red;">Couldn't get result from %s color: red;">n color: #8b0000;">" color: gray;">, color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_error color: olive;">( color: blue;">sock color: olive;">)) color: gray;">;
        
color: blue;">exit color: olive;">( color: maroon;">1 color: olive;">) color: gray;">;
    
color: olive;">} color: gray;">
    
    
color: blue;">printf color: olive;">( color: #8b0000;">" color: red;">number of fields returned: %d color: red;">n color: #8b0000;">" color: gray;">, color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_num_fields color: olive;">( color: blue;">res color: olive;">)) color: gray;">;
        
    
color: green;">while color: gray;"> color: olive;">( color: blue;">row color: gray;"> = color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_fetch_row color: olive;">( color: blue;">res color: olive;">)) color: gray;"> color: olive;">{ color: gray;">
        
color: blue;">printf color: olive;">( color: #8b0000;">" color: red;">Ther userid #%d 's username is: %s color: red;">n color: #8b0000;">" color: gray;">, color: blue;">atoi color: olive;">( color: blue;">argv color: olive;">[ color: maroon;">1 color: olive;">]) color: gray;">, color: olive;">((( color: blue;">row color: olive;">[ color: maroon;">0 color: olive;">] color: gray;">== class="hl-prepro">NULL color: olive;">) color: gray;">&& color: olive;">( color: gray;">! color: blue;">strlen color: olive;">( color: blue;">row color: olive;">[ color: maroon;">0 color: olive;">]))) color: gray;"> ? color: #8b0000;">" color: red;">NULL color: #8b0000;">" color: gray;"> : color: blue;">row color: olive;">[ color: maroon;">0 color: olive;">])) color: gray;"> ;
        
color: blue;">puts color: olive;">( color: gray;"> color: #8b0000;">" color: red;">query ok ! color: red;">n color: #8b0000;">" color: gray;"> color: olive;">) color: gray;"> ;
    
color: olive;">} color: gray;">
    
    
color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_free_result color: olive;">( color: blue;">res color: olive;">) color: gray;">;
    
color: blue;">class="tags" href="/tags/MYSQL.html" title=mysql>mysql_close color: olive;">( color: blue;">sock color: olive;">) color: gray;">;
    
color: blue;">exit color: olive;">( color: maroon;">0 color: olive;">) color: gray;">;
    
color: green;">return color: gray;"> color: maroon;">0 color: gray;">;    color: #ffa500;">//. 为了兼容大部分的编译器加入此行 color: gray;">
color: olive;">}

编译的时候࿰c;使用下面的命令

class="hl-surround">
class="hl-main"> gcc -o class="tags" href="/tags/MYSQL.html" title=mysql>mysql_select ./class="tags" href="/tags/MYSQL.html" title=mysql>mysql_select.c -I/usr/local/include/class="tags" href="/tags/MYSQL.html" title=mysql>mysql -L/usr/local/lib/class="tags" href="/tags/MYSQL.html" title=mysql>mysql -lclass="tags" href="/tags/MYSQL.html" title=mysql>mysqlclient (-lz) (-lm) 后面两个选项可选࿰c;根据您的环境情况

运行的时候࿰c;执行下面的命令

class="hl-surround">
class="hl-main"> ./class="tags" href="/tags/MYSQL.html" title=mysql>mysql_select 1

将返回如下结果:

class="hl-surround">
class="hl-main"> number of fields returned: 1
Ther userid #1 's username is: Michael
query ok !

上面的代码我想大部分都能看明白࿰c;不明白的可以参考一下MySQL提供的有关Cclass="tags" href="/tags/YuYan.html" title=语言>语言API部分文档࿰c;各个函数都有详细说明࿰c;有时间我整理一份常用的 API说明出来。

cle>

http://www.niftyadmin.cn/n/1605207.html

相关文章

刷脸支付顺应时代各种优惠政策出现

相比于人工合成的二维码扫码支付&#xff0c;刷脸支付采用的是生物信息识别技术&#xff0c;在安全性上后者要比前者高很多。刷脸支付自从出世以来就受到广大创业者、商家的关注&#xff0c;自从去年支付宝推出刷脸支付并在实体店投入运营&#xff0c;到今年刷脸支付得到快速的…

重磅!百度智能云获开源基础设施峰会超级用户大奖

11月4日&#xff0c;开源基础设施峰会在上海隆重举行&#xff0c;这是OpenStack峰会改名开源基础设施峰会后首次登陆中国。此次大会吸引了来自世界各地的上万名开源用户和贡献者精英&#xff0c;共同探讨开源技术与基础设施的发展方向。作为峰会最大的亮点之一&#xff0c;超级…

刷脸支付人脸识别能够大幅度地提升速度

刷脸支付能够吸引更多的消费群体。目前&#xff0c;餐饮行业的消费主力军为85后至90后&#xff0c;这个年龄段的消费者比较喜欢新颖、时尚的事物。商家使用刷脸支付&#xff0c;除了能够吸引更多的年轻消费群体以外&#xff0c;还能照顾部分老年朋友&#xff0c;这部分人对电子…

“金融上链”正当时,百度智能云推出“链上可信金融”解决方案

政策东风拂过&#xff0c;区块链再度登上风口浪尖。峰回路转&#xff0c;区块链行业迎来全面发展的新时代&#xff0c;越来越多的行业正在打造垂直领域的区块链解决方案。其中&#xff0c;最先拥抱且融合程度最高的金融行业&#xff0c;占据了区块链应用落地的半壁江山。百度作…

中国游戏业遭遇十年之痒 严肃游戏成新增长点

http://www.chinanews.com.cn/cj/2010/11-26/2683501.shtml 从世纪之初便迅速红火的中国网游产业&#xff0c;终于在迈入第10个年头时迎来了些许的疲软。而在单机游戏阵地骤缩、电子竞技难以崛起的大环境下&#xff0c;以B2B、B2G模式为主、曾在泛娱乐化大潮中一直处于边缘位置…

刷脸支付效率提高商家的经营成本大大降低

人脸识别其实是一种生物特征识别技术&#xff0c;目前我国的人脸识别技术研究和应用已经比较成熟&#xff0c;处于国际领先地位&#xff0c;在国际权威人脸识别公开评测集合LFW上&#xff0c;算法识别率竞赛前几名均由国内的科技公司包揽。目前人脸识别技术领域所面临的大技术难…

李彦宏会晤中国工商银行董事长陈四清

2019年11月7日&#xff0c;中国工商银行董事长陈四清率队走访百度公司&#xff0c;与百度创始人、董事长兼CEO李彦宏就进一步深化银企合作、共同探索金融科技创新与应用等议题进行深入交流。李彦宏对陈四清一行的到访表示热烈欢迎&#xff0c;陪同参观了百度科技展厅&#xff0…

微信支付宝刷脸支付竞争已经枕戈待旦

互联网在进入中国的第25个年头&#xff0c;20多年来&#xff0c;中国互联网的流量一直处于总量井喷的状态&#xff0c;PC普及到网络普及&#xff0c;再到智能手机普及&#xff0c;4G普及再到5G 入场&#xff0c;链接入口越来越多&#xff0c;近五年间&#xff0c;网红电商、新零…