📄 vc++中使用capi连接mysql(转载)_花谢花会开.htm
字号:
<!--STATUS OK-->
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<base href="http://hi.baidu.com/justin_jia/blog/item/7873eeec56b7cb2663d09f37.html">
<style>
body{margin:4px 0}
#bd_sn_h{text-align:left;background-color:#ffffff;color:#000000}
#bd_sn_h #p1{clear:both;font:14px Arial;margin:0 0 0 2px;padding:4px 0 0 0}
#bd_sn_h a{color:#0000ff;text-decoration:underline}
#bd_sn_h #p1 a{font-weight:bold}
#baidu div{position:static}
</style>
<table id="baidu" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<div style="text-align:left;background-color:#ffffff;color:#000000">
<div style="margin:6px 18px 0 10px;float:left"><a href="http://www.baidu.com"><img style="border:0px" alt="到百度首页" src="http://img.baidu.com/img/logo-kz.gif"></a></div>
<div style="margin:27px 0 0 0;float:left">
<form style="margin:0;padding:0" action="http://www.baidu.com/s">
<input name="wd" size="35" style="font:16px Arial"> <input type="submit" value="百度一下">
<input name="cl" type="hidden" value="3">
</form>
</div>
<p style="clear:both;font:14px Arial;margin:0 0 0 2px;padding:4px 0 0 0;width:100%;text-align:left;background-color:#ffffff;color:#000000">您查询的关键词是:<B style='color:black;background-color:#ffff66'><a href=http://cache.baidu.com/c?word=vc%3B%C1%AC%BD%D3%3Bmysql&url=http%3A//hi%2Ebaidu%2Ecom/justin%5Fjia/blog/item/7873eeec56b7cb2663d09f37%2Ehtml&p=8c64c70796904eaf5fbd9b7c4943&user=baidu#baidusnap0>vc</a> </B><B style='color:black;background-color:#A0FFFF'><a href=http://cache.baidu.com/c?word=vc%3B%C1%AC%BD%D3%3Bmysql&url=http%3A//hi%2Ebaidu%2Ecom/justin%5Fjia/blog/item/7873eeec56b7cb2663d09f37%2Ehtml&p=8c64c70796904eaf5fbd9b7c4943&user=baidu#baidusnap1>连接mysql</a> </B> 。如果打开速度慢,可以尝试<a style="color:#0000ff;text-decoration:underline;font-weight:bold" href="http://cache.baidu.com/c?word=vc%3B%C1%AC%BD%D3%3Bmysql&url=http%3A//hi%2Ebaidu%2Ecom/justin%5Fjia/blog/item/7873eeec56b7cb2663d09f37%2Ehtml&p=8c64c70796904eaf5fbd9b7c4943&user=baidu&fast=y">快速版</a>;如果想保存快照,可以<a style="color:#0000ff;text-decoration:underline;font-weight:bold" onclick="window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title)+'&iu='+encodeURIComponent(location.href)+'&fr=ps#nw=1','_s','scrollbars=no,width=600,height=450,right=75,top=20,status=no,resizable=yes'); return false;" href="http://cang.baidu.com/do/add" target="_blank">添加到搜藏</a>。</p>
<p style="font:12px Arial;color:gray;margin:0 2px;width:100%text-align:left;background-color:#ffffff">(百度和网页<a style="color:#0000ff;text-decoration:underline" href="http://hi.baidu.com/justin_jia/blog/item/7873eeec56b7cb2663d09f37.html">http://hi.baidu.com/justin_jia/blog/item/7873eeec56b7cb2663d09f37.html</a>的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。)</p>
<hr style="margin:8px 0;width:100%">
</div>
</td></tr>
</table>
<div style="position:relative">
<!--STATUS OK-->
<html><head><meta http-equiv=content-type content="text/html; charset=gb2312"><title>VC++中使用CAPI连接MySql(转载)_花谢花会开</title>
<style>.error{color:#FF0000;font-size:12px}</style>
</head>
<body onLoad="formatonlinpic();">
<center>
<link rel="stylesheet" type="text/css" href="/justin%5Fjia/css/item/1225d95123a71b2442a75ba4.css">
<link rel="stylesheet" type="text/css" href="/space.css">
<style type="text/css">
#usrbar{padding:4px 10px 3px 0;font-size:12px;height:19px;line-height:19px;color:#000000;font-family:Arial;text-align:right;background:#ffffff;filter:alpha(opacity=65);-moz-opacity:0.5;width:auto !important;width:100%;letter-spacing:normal}
#usrbar a,#usrbar a:link,#usrbar a:visited{color:#0000CC;text-decoration:underline}
#ft{clear:both;height:20px;line-height:20px;color:#666666;font-size:12px;font-family:Arial;text-align:center}
#ft a,#ft a:link,#ft a:visited{color:#7777CC;text-decoration:underline}
#usrbar,#usrbar a,#usrbar a:link,#usrbar a:visited,#ft,#ft a,#ft a:link,#ft a:visited{letter-spacing:normal}
</style>
<div id="usrbar"><nobr>
<a href="http://www.baidu.com/" target="_blank">百度首页</a> | <a href="http://hi.baidu.com" target="_blank">百度空间</a>
</nobr></div>
<div id="main" align="left">
<!--[if IE]>
<![endif]-->
<div id="header">
<div class="lc"><div class="rc"></div></div>
<div class="tit"><a href="/justin%5Fjia" class="titlink" title="justin314的空间 http://hi.baidu.com/justin_jia">花谢花会开</a></div>
<div class="desc"></div>
<div id="tabline"> </div>
<div id="tab"><a href="/justin%5Fjia">主页</a><a href="/justin%5Fjia/blog" class="on">博客</a><a href="/justin%5Fjia/album">相册</a><span>|</span><a href="/justin%5Fjia/profile">个人档案</a>
<span>|</span><a href="/justin%5Fjia/friends">好友</a>
</div>
</div>
<div class="stage">
<div class="stagepad">
<div style="width:100%">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="modth">
<tr><td class="modtl" width="7"> </td>
<td class="modtc" nowrap><div class="modhead"><span class="modtit">查看文章</span></div></td>
<td class="modtc" nowrap align="right"></td>
<td class="modtr" width="7"> </td>
</tr></table>
<div id="m_blog" class="modbox">
<div class="tit"><a name=baidusnap0></a><B style='color:black;background-color:#ffff66'>VC</B>++中使用CAPI<a name=baidusnap1></a><B style='color:black;background-color:#A0FFFF'>连接MySql</B>(转载)</div>
<div class="date">2007-05-17 21:43</div>
<table style="table-layout:fixed"><tr><td><div class="cnt"><table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellspacing="0" cellpadding="0" width="760" bgcolor="#ffffff" border="0"><tbody> <tr> <td align="center" height="25"><p align="left"> </p></td> </tr> <tr> <td align="center" height="9"><p align="left"> </p></td> </tr> <tr> <td align="center"><table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellspacing="0" cellpadding="0" width="740" border="0"> <tbody> <tr> <td width="740"><div style="MARGIN: 15px" width="560">连接的代码是重网上弄来的,<B style='color:black;background-color:#ffff66'>VC</B>6+MySql5.0</div> <div style="MARGIN: 15px" width="560">#include <windows.h><br>#include <iostream><br>#include <mysql.h><br>#include <stdio.h></div> <div style="MARGIN: 15px" width="560">using namespace std;<br>int main()<br>{<br> //connection params<br>FILE *file;<br> char *host = "localhost";<br> char *user = "root";<br> char *pass = "123456";<br> char *db = "movee";<br> //sock<br> MYSQL *sock;<br> MYSQL_RES *results;<br> MYSQL_ROW record;<br><br> sock = mysql_init(0);<br> if (sock) cout << "sock handle ok!" << endl;<br> else {<br> cout << "sock handle failed!" << mysql_error(sock) << endl;<br> }<br> //connection<br> if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))<br> cout << "connection ok!" << endl;<br> else {<br> cout << "connection fail: " << mysql_error(sock) << endl;<br> }<br>mysql_set_character_set(sock,"gb2312");<br> //connection character set<br> cout << "connection character set: " << mysql_character_set_name(sock) << endl;<br> //wait for posibility to check system/mysql sockets<br> <br> if(mysql_query(sock,"select * from admin_user"))<br> {<br> cout<<"very good!"<<endl; <br> }<br> results=mysql_store_result(sock);<br>printf("用户名\t等级\n");<br> while(record=mysql_fetch_row(results))<br> {<br> printf("%s\t%s \n",record[1],record[3]);<br> <br> }<br> mysql_free_result(results);<br> system("PAUSE");<br> <br> //closing connection<br> mysql_close(sock);<br> return EXIT_SUCCESS;<br>}</div> <div style="MARGIN: 15px" width="560"></div> <div style="MARGIN: 15px" width="560"></div> <div style="MARGIN: 15px" width="560">我的mysql安装路径为d:\software\mysql5.0</div> <div style="MARGIN: 15px" width="560">所以要在<B style='color:black;background-color:#ffff66'>VC</B>中设置include路径和lib的路径。添加MySql的include目录到<B style='color:black;background-color:#ffff66'>VC</B>工作台中</div> <div style="MARGIN: 15px" width="560">Project->Settings->C/C++->Category->Preprocessor->Additional include directories中添加:D:\software\mysql5.0\include。</div> <div style="MARGIN: 15px" width="560">添加lib的路径:Tools->Options->Directories中选择Library files,然后添加lib的目录:D:\SOFTWARE\MYSQL5.0\LIB</div> <div style="MARGIN: 15px" width="560">然后在Project->Settings->Link中,添加libmysql.lib到Object/libray modules中。还要确保Project Options中为/subsystem:console而不是/subsystem:window,否则编译错误。<br></div></td> </tr> </tbody> </table></td></tr></tbody></table></div></td></tr></table>
<br>
<div class="opt">
<a href="/justin%5Fjia/blog/category/%B2%D8%BE%AD%B8%F3" title="查看该分类中所有文章">类别:藏经阁</a>
| <a title="将此文章添加到百度搜藏" href="http://cang.baidu.com/do/add" onClick="window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent('VC++中使用CAPI连接MySql(转载)'+'_百度空间')+'&iu='+encodeURIComponent(location.href)+'&fr=sp#nw=1','_s','scrollbars=no,width=600,height=450,right=75,top=20,status=no,resizable=yes'); return false;" target="_blank">添加到搜藏</a>
| 浏览(<span id="result"></span>)
</div>
<div class="line"> </div>
<div id="in_comment">
<a name="comment"></a>
<div class="tit">网友评论:</div>
<div id="page"></div>
</div>
<div id="in_send">
<form name="form1" id="popFormSubmit" action="/justin%5Fjia/commit" method="post" onSubmit="return checkcmtform()">
<input type="hidden" name="ct" value="8">
<input type="hidden" name="cm" value="1">
<input type="hidden" name="spBlogID" value="7873eeec56b7cb2663d09f37">
<div class="tit">发表评论:</div>
<table width="620" border="0" cellspacing="5" cellpadding="0">
<tr>
<td class="f14">姓 名:</td>
<td><input type="text" name="spBlogCmtor" id="spBlogCmtor" style="width:220px" onChange="checkname('spBlogCmtor')" maxlength="49" onfocus="hidErr(1);"><div style="display:none" id="nmerror">*姓名最长为50字节</div></td>
</tr>
<tr id="1_err" style="display:none">
<td> </td>
<td><div class="error" id="1_err_con"></div></td>
</tr>
<tr>
<td class="f14">网址或邮箱:</td>
<td><input type="text" name="spBlogCmtURL" id="spBlogCmtURL" style="width:360px" maxlength="128" onChange="checkeandu('spBlogCmtURL')" onfocus="hidErr(2);"> (选填)</td>
</tr>
<tr id="2_err" style="display:none">
<td> </td>
<td><div class="error" id="2_err_con"></div></td>
</tr>
<tr>
<td valign="top" class="f14">内 容:</td>
<td><textarea name="spBlogCmtText" id="spBlogCmtText" style="width:520px;height:155px" onfocus="showverkey();hidErr(3);"></textarea>
</td>
</tr>
<tr id="3_err" style="display:none">
<td> </td>
<td><div class="error" id="3_err_con"></div></td>
</tr>
<tr id="vercode">
<td valign="top" class="f14">验证码:</td>
<td valign="top"><input type="hidden" name="spVcode" value="63F2670E479E032B23642F071D838C1F1B28683D8A835CA420634674E015DCCE8743D4D3BB4DAE6963CD000827E3E5DA37FE288DDE80B9A52A254AFE0CF03561" />
<input type="text" name="spVerifyKey" size="6" maxlength="4" autocomplete="off">请输入下图中的四位验证码,字母不区分大小写。<br>
<a href="javascript:newverifypic();" title="看不清左边的字符">看不清?</a>
</td>
</tr>
<tr>
<td valign="top"class="f14"> </td>
<td valign="top" class="f14"><input name="btn_ok" type="submit" value="发表评论"></td>
</tr>
</table>
</form>
</div>
<br>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="8">
<tr><td class="modbl" width="7"> </td>
<td class="modbc"> </td>
<td class="modbr" width="7"> </td>
</tr></table>
</div>
</div>
</div>
</div>
<br><center><div id="ft">©2007 Baidu</div></center>
</center>
<img src="http://c.baidu.com/c.gif?t=6&p=3" style="display:none;"/>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -