<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[用C语言操作MySQL数据库，进行连接、插入、修改、删除等操作]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 11 Mar 2010 12:24:25 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	很多人用到MySQL来开发一些项目，有时为了性能，我们会直接用C语言来开发相关的模块，尤其在我们的web应用中，虽然PHP、JSP等脚本均提供了MySQL的接口，但是显然直接使用C语言具有更好的安全性和性能，Michael以前用PHP开发的多个项目中就使用了C语言编写的这类接口，然后再编译到php里面，供php脚本直接使用,这方面的话题就不多说了，下面主要说一下在Linux下如何用C语言连接MySQL数据库，并且读取里面的数据返回，同时如何进行编译。<br/><br/>　　这里的大部分代码参考了MySQL发行包里面的.c源文件，大家也可以去里面找找相关的代码，下面这段代码实现了连接到本地MySQL服务器上9tmd_bbs_utf8数据库，从数据表tbb_user中根据输入的userid取得该用户的用户名并打印输出到终端。<br/><br/><div class="code"><br/><br/>#if defined(_WIN32) &#124;&#124; defined(_WIN64)&nbsp;&nbsp;//为了支持windows平台上的编译<br/>#include &lt;windows.h&gt;<br/>#endif<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &quot;mysql.h&quot;&nbsp;&nbsp;//我的机器上该文件在/usr/local/include/mysql下<br/> <br/>//定义数据库操作的宏，也可以不定义留着后面直接写进代码<br/>#define SELECT_QUERY &quot;select username from tbb_user where userid = %d&quot;<br/> <br/>int main(int argc, char **argv) //char **argv 相当于 char *argv&#91;&#93;<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;MYSQL mysql,*sock;&nbsp;&nbsp;&nbsp;&nbsp;//定义数据库连接的句柄，它被用于几乎所有的MySQL函数<br/>&nbsp;&nbsp;&nbsp;&nbsp;MYSQL_RES *res;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //查询结果集，结构类型<br/>&nbsp;&nbsp;&nbsp;&nbsp;MYSQL_FIELD *fd ;&nbsp;&nbsp;&nbsp;&nbsp; //包含字段信息的结构<br/>&nbsp;&nbsp;&nbsp;&nbsp;MYSQL_ROW row ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //存放一行查询结果的字符串数组<br/>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;qbuf&#91;160&#93;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//存放查询sql语句字符串<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (argc != 2) &#123;&nbsp;&nbsp;//检查输入参数<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&quot;usage : mysql_select &lt;userid&gt;&#92;n&#92;n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;mysql_init(&amp;mysql);<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (!(sock = mysql_real_connect(&amp;mysql,&quot;localhost&quot;,&quot;dbuser&quot;,&quot;dbpwd&quot;,&quot;9tmd_bbs_utf8&quot;,0,NULL,0))) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&quot;Couldn&#039;t connect to engine!&#92;n%s&#92;n&#92;n&quot;,mysql_error(&amp;mysql));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sprintf(qbuf,SELECT_QUERY,atoi(argv&#91;1&#93;));<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(mysql_query(sock,qbuf)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&quot;Query failed (%s)&#92;n&quot;,mysql_error(sock));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (!(res=mysql_store_result(sock))) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&quot;Couldn&#039;t get result from %s&#92;n&quot;, mysql_error(sock));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;number of fields returned: %d&#92;n&quot;,mysql_num_fields(res));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;while (row = mysql_fetch_row(res)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Ther userid #%d &#039;s username is: %s&#92;n&quot;, atoi(argv&#91;1&#93;),(((row&#91;0&#93;==NULL)&amp;&amp;(!strlen(row&#91;0&#93;))) ? &quot;NULL&quot; : row&#91;0&#93;)) ; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts( &quot;query ok !&#92;n&quot; ) ; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125; <br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;mysql_free_result(res);<br/>&nbsp;&nbsp;&nbsp;&nbsp;mysql_close(sock);<br/>&nbsp;&nbsp;&nbsp;&nbsp;exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp; //. 为了兼容大部分的编译器加入此行<br/>&#125;<br/></div><br/><br/>编译的时候，使用下面的命令<br/><br/><div class="code">gcc -o mysql_select ./mysql_select.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient (-lz) (-lm) 后面两个选项可选，根据您的环境情况</div><br/><br/>运行的时候，执行下面的命令<br/><br/><br/><div class="code">./mysql_select 1</div>将返回如下结果：<br/><br/><br/><div class="code">number of fields returned: 1<br/>Ther userid #1 &#039;s username is: Michael</div><br/>query ok !上面的代码我想大部分都能看明白，不明白的可以参考一下MySQL提供的有关C语言API部分文档，各个函数都有详细说明，有时间我整理一份常用的API说明出来。<br/>来源：<br/>http://www.toplee.com/blog/329.html
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 用C语言操作MySQL数据库，进行连接、插入、修改、删除等操作]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>