📄 vc使用c api 连接操作mysql数据库[中文博客群-文章摘要].htm
字号:
</div>
</div>
<div id="right_pannel" style="width: 660px;">
<div class="t3 bc_title_green">
文章摘要信息</div>
<div class="t4 bc_content_green">
<table width="100%">
<tr>
<td width="100">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td height="66" class="dig_tuijian" align="center">
<div id="tuijian_num" style="z-index: 99">
0
</div>
</td>
</tr>
<tr>
<td align="center">
<div id="tuijian_message">
<a href="javascript:tuijian()">顶</a></div>
</td>
</tr>
</table>
</td>
<td>
<h3>
<a name=baidusnap4></a><B style='color:black;background-color:#ff66ff'>VC</B>使用<a name=baidusnap1></a><B style='color:black;background-color:#A0FFFF'>C</B> <a name=baidusnap2></a><B style='color:black;background-color:#99ff99'>API</B> <B style='color:black;background-color:#ff9999'>连接</B>操作<a name=baidusnap0></a><B style='color:black;background-color:#ffff66'>MySQL</B>数据库
</h3>
<div>
作者:<a href="../2094.html" target="_blank">隔叶黄莺 The Blog of Unmi</a></div>
<div>
发表于:2007-7-24 1:59:00</div>
<div style="color: Gray;">
如果觉得文章不错,请点击左侧的“顶”来推荐此文章。</div>
</td>
</tr>
</table>
<div id="googlead" style="position: relative; float: left; margin: 5px;">
</div>
<P>一切尽在代码中,代码中也太多了if/else,可以对它进行更好的函数及至类的封装,规范的处理好异常。</P>
<DIV class=dp-highlighter>
<DIV class=bar></DIV>
<OL class=dp-cpp>
<LI class=alt><SPAN><SPAN class=preprocessor>#include <windows.h><STDIO.H></SPAN></SPAN>
<LI class=""><SPAN></SPAN><SPAN class=comment>//需要在<B style='color:black;background-color:#ff66ff'>VC</B>的Options设置一个include路径指向''%<B style='color:black;background-color:#ffff66'>mysql</B>_home%/inlude''目录 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN></SPAN><SPAN class=preprocessor>#include <<B style='color:black;background-color:#ffff66'>mysql</B>.h><MYSQL.H></SPAN></SPAN>
<LI class=""><SPAN></SPAN><SPAN class=comment>//设置一个lib路径指向''%<B style='color:black;background-color:#ffff66'>mysql</B>_home%/lib/opt''目录 (<B style='color:black;background-color:#ffff66'>mysql</B>5.0是个目录) </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN></SPAN><SPAN class=preprocessor>#pragma comment(lib,"libmysql.lib") </SPAN><SPAN> </SPAN></SPAN>
<LI class=""><SPAN></SPAN><SPAN class=preprocessor>#define host_name "localhost" //数据库服务器 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN></SPAN><SPAN class=preprocessor>#define db_name "test" //数据库名 </SPAN><SPAN> </SPAN></SPAN>
<LI class=""><SPAN></SPAN><SPAN class=preprocessor>#define user_name "root" //用户名 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN></SPAN><SPAN class=preprocessor>#define password "" //密码 </SPAN><SPAN> </SPAN></SPAN>
<LI class=""><SPAN></SPAN><SPAN class=datatypes>int</SPAN><SPAN> main(</SPAN><SPAN class=datatypes>int</SPAN><SPAN> argc,</SPAN><SPAN class=datatypes>char</SPAN><SPAN> * argv[]) { </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=datatypes>char</SPAN><SPAN> szSqlText[500] ; </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>MYSQL</B> *conn; </SPAN>
<LI class=alt><SPAN> <B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *rs; </SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW row; </SPAN><SPAN class=comment>//注意它的声明 typedef char **<B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW,字符串数组 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=datatypes>BOOL</SPAN><SPAN> bCreate = FALSE; </SPAN></SPAN>
<LI class=""><SPAN> </SPAN>
<LI class=alt><SPAN> conn = <B style='color:black;background-color:#ffff66'>mysql</B>_init(NULL); </SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN>(conn == NULL) </SPAN></SPAN>
<LI class=alt><SPAN> { </SPAN>
<LI class=""><SPAN> fprintf(stderr,</SPAN><SPAN class=string>"<B style='color:black;background-color:#ffff66'>mysql</B>_init() failed (probably out of memory)\n"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=alt><SPAN> exit(1); </SPAN>
<LI class=""><SPAN> } </SPAN>
<LI class=alt><SPAN> </SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN> (<B style='color:black;background-color:#ffff66'>mysql</B>_real_connect(conn,host_name,user_name,password, </SPAN></SPAN>
<LI class=alt><SPAN> db_name,<B style='color:black;background-color:#ffff66'>MYSQL</B>_PORT,NULL,0) == NULL) </SPAN>
<LI class=""><SPAN> { </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=comment>//在<B style='color:black;background-color:#ffff66'>MYSQL</B>初始化之后的操作如果有错误,可以用<B style='color:black;background-color:#ffff66'>mysql</B>_errno(<B style='color:black;background-color:#ffff66'>MYSQL</B>*)和 </SPAN><SPAN> </SPAN></SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=comment>//<B style='color:black;background-color:#ffff66'>mysql</B>_errer(<B style='color:black;background-color:#ffff66'>MYSQL</B>*) 分别获得出错代号和描述 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> fprintf(stderr,</SPAN><SPAN class=string>"<B style='color:black;background-color:#ffff66'>mysql</B>_real_connect() failed:\nError %u (%s)\n"</SPAN><SPAN>, </SPAN></SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>mysql</B>_errno(conn),<B style='color:black;background-color:#ffff66'>mysql</B>_error(conn)); </SPAN>
<LI class=alt><SPAN> exit(1); </SPAN>
<LI class=""><SPAN> } </SPAN>
<LI class=alt><SPAN> </SPAN>
<LI class=""><SPAN> printf(</SPAN><SPAN class=string>"connect to db successful.\n"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN> (bCreate) { </SPAN></SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=comment>//第一次运行创建一个表mytable </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> sprintf(szSqlText,</SPAN><SPAN class=string>"create table mytable(time datetime,s1 char(6),s2 char(11),s3 int,s4 int)"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN> (<B style='color:black;background-color:#ffff66'>mysql</B>_query(conn,szSqlText)) { </SPAN></SPAN>
<LI class=alt><SPAN> printf(</SPAN><SPAN class=string>"Can''t create table.\n"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>mysql</B>_close(conn); </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>return</SPAN><SPAN> 0; </SPAN></SPAN>
<LI class=""><SPAN> } </SPAN>
<LI class=alt><SPAN> } </SPAN>
<LI class=""><SPAN> sprintf(szSqlText,</SPAN><SPAN class=string>"insert into mytable values(''2000-3-10 21:01:30'',''Test'',''MySQLTest'',2000,3)"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN> (<B style='color:black;background-color:#ffff66'>mysql</B>_query(conn,szSqlText)) { </SPAN></SPAN>
<LI class=""><SPAN> printf(</SPAN><SPAN class=string>"Insert values error:\nError %u (%s)\n"</SPAN><SPAN>, </SPAN></SPAN>
<LI class=alt><SPAN> <B style='color:black;background-color:#ffff66'>mysql</B>_errno(conn),<B style='color:black;background-color:#ffff66'>mysql</B>_error(conn)); </SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>mysql</B>_close(conn); </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>return</SPAN><SPAN> 0; </SPAN></SPAN>
<LI class=""><SPAN> } </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>else</SPAN><SPAN>{ </SPAN></SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=comment>//insert/delete/update 语句可用<B style='color:black;background-color:#ffff66'>mysql</B>-affected_rows()得到受作用的行 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> printf(</SPAN><SPAN class=string>"INSERT statement succeeded: %lu rows affected\n"</SPAN><SPAN>, </SPAN></SPAN>
<LI class=""><SPAN> (unsigned </SPAN><SPAN class=datatypes>long</SPAN><SPAN>)<B style='color:black;background-color:#ffff66'>mysql</B>_affected_rows(conn)); </SPAN></SPAN>
<LI class=alt><SPAN> } </SPAN>
<LI class=""><SPAN> </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=comment>//查询数据 </SPAN><SPAN> </SPAN></SPAN>
<LI class=""><SPAN> sprintf(szSqlText,</SPAN><SPAN class=string>"select * from mytable"</SPAN><SPAN>); </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=comment>//执行成功则返回零 </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>if</SPAN><SPAN> (<B style='color:black;background-color:#ffff66'>mysql</B>_query(conn,szSqlText) != 0) { </SPAN></SPAN>
<LI class=""><SPAN> <B style='color:black;background-color:#ffff66'>mysql</B>_close(conn); </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>return</SPAN><SPAN> 0; </SPAN></SPAN>
<LI class=""><SPAN> } </SPAN>
<LI class=alt><SPAN> </SPAN><SPAN class=keyword>else</SPAN><SPAN> { </SPAN></SPAN>
<LI class=""><SPAN> </SPAN><SPAN class=comment>//立即从服务器返回所有行,存储到本地,产生结果集,失败则返回NULL </SPAN><SPAN> </SPAN></SPAN>
<LI class=alt><SPAN> rs = <B style='color:black;background-color:#ffff66'>mysql</B>_store_result(conn); </SPAN>
<LI class=""
<div id="googlead2" style="position: relative; float: left; margin: 5px;">
</div>
<p style="clear: both;">
<div style="display: none">
</div>
<div>
<strong>收藏到网摘:</strong>
</div>
<br />
</p>
<p style="clear: both;">
<br />
<br />
原文链接<a href="#" onclick="window.open('http://unmi.blogcn.com/diary,8940952.shtml')" target="_self">http://unmi.blogcn.com/diary,8940952.shtml</a></p>
</div>
<p>
</p>
</div>
<div id="googleadcode" style="display: none">
</div>
<div id="googleadcode2" style="display: none">
<br>
<br>
</div>
</form>
</div>
</div>
<div id="bottompannel">
<hr color="#dfdfdf">
<div id="googleadTop" style="background-color: White;" width="728" height="15" align="center">
</div>
<p align="center">
有问题加QQ群: ①:11769158满 ②:24143195 ③:13156479 ④:27077846 ⑤:27077937 ⑥:28232202 ⑦:16224919
⑧:24526052</p>
<p align="center">
中文博客群 版权所有,未经授权禁止复制或建立镜像。 <a href="http://www.miibeian.gov.cn" target="_blank">辽ICP备06009670号</a>
| 联系我们:<a href="mailto:webmaster#bokequn.cn">webmaster#bokequn.cn</a> | <a class="red" href="/jubao.aspx">举报该页面</a> |
</p>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -