{"id":86,"date":"2013-01-29T11:56:35","date_gmt":"2013-01-29T03:56:35","guid":{"rendered":"http:\/\/www.datarelab.com\/blog\/?p=86"},"modified":"2013-01-29T11:56:35","modified_gmt":"2013-01-29T03:56:35","slug":"vc%e8%bf%9e%e6%8e%a5mysql","status":"publish","type":"post","link":"https:\/\/www.datarelab.com\/blog\/Technical_literature\/86.html","title":{"rendered":"VC\u8fde\u63a5MySQL"},"content":{"rendered":"<p>\u4e00\u3001MySQL\u7684\u5b89\u88c5<br \/>\n\u6ce8\u610f\u9009\u62e9\u201c\u5b8c\u5168\u5b89\u88c5\u201d\uff08\u53ea\u6709\u8fd9\u6837\u624d\u4f1a\u5b89\u88c5VC\u7f16\u8bd1\u65f6\u9700\u8981\u7684\u5934\u6587\u4ef6\u7b49\uff09\u3002\u5b89\u88c5\u540e\u671f\u4f1a\u8fdb\u884c\u670d\u52a1\u5668\u914d\u7f6e\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4f60\u7684\u670d\u52a1\u5668\u767b\u9646\u5bc6\u7801\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u8bbe\u7f6e\u5bc6\u7801\u3002<\/p>\n<p>\u4e8c\u3001VC6.0\u7684\u8bbe\u7f6e<\/p>\n<p>\uff081\uff09\u6253\u5f00VC6.0 \u5de5\u5177\u680fTools\u83dc\u5355\u4e0b\u7684Options\u9009\u9879\uff0c\u5728Directories\u7684\u6807\u7b7e\u9875\u4e2d\u53f3\u8fb9\u7684\u201cShow directories for:\u201d\u4e0b\u62c9\u5217\u8868\u4e2d\u9009\u4e2d\u201cIncludefiles\u201d\uff0c\u7136\u540e\u5728\u4e2d\u95f4\u5217\u8868\u6846\u4e2d\u6dfb\u52a0\u4f60\u672c\u5730\u5b89\u88c5MySQL\u7684include\u76ee\u5f55\u8def\u5f84\u3002\uff08\u6211\u7684\u662fD:\\Program Files\\MySQL\\MySQL Server 5.0\\include\uff09\u3002<\/p>\n<p>\uff082\uff09\u5728\u4e0a\u9762\u8bf4\u5230\u7684\u201cShow directories for:\u201d\u4e0b\u62c9\u5217\u8868\u4e2d\u9009\u4e2d\u201cLibrary files\u201d\uff0c\u7136\u540e\u6dfb\u52a0\u4f60\u672c\u5730\u5b89\u88c5MySQL\u7684Lib\u76ee\u5f55\u8def\u5f84\u3002Lib\u76ee\u5f55\u4e0b\u8fd8\u6709debug\u548copt\u4e24\u4e2a\u76ee\u5f55\uff0c\u5efa\u8bae\u9009debug\u3002\uff08\u6211\u7684\u662fD:\\Program Files\\MySQL\\MySQL Server 5.0\\lib\\debug\uff09\u3002<\/p>\n<p>\uff083\uff09\u5728\u201cProject settings-&gt;Link:Object\/library modules\u201d\u91cc\u9762\u6dfb\u52a0\u201clibmysql.lib\u201d\u3002<\/p>\n<p>\uff084\uff09\u5728stdafx.h\u91cc\u9762\u6dfb\u52a0\u5982\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<p>#include \"mysql.h\"<br \/>\n#include \"winsock.h\"&nbsp;&nbsp; \/\/ \u5982\u679c\u7f16\u8bd1\u51fa\u9519,\u5219\u628a\u8be5\u884c\u653e\u5230#include \"mysql.h\"\u4e4b\u524d<br \/>\n#pragma comment(lib,\"libmySQL.lib\")&nbsp;&nbsp; \/\/ \u5982\u679c\u5728\u9644\u52a0\u4f9d\u8d56\u9879\u91cc\u5df2\u589e\u52a0,\u5219\u5c31\u4e0d\u8981\u6dfb\u52a0\u4e86<br \/>\n\uff085\uff09\u5efa\u8bae\u5c06\u201clibmySQL.lib\u3001libmySQL.dll\u201d\u62f7\u5230\u4f60\u6240\u5efa\u7684\u5de5\u7a0b\u7684\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u4e09\u3001\u6570\u636e\u5e93\u3001\u8868\u7684\u521b\u5efa<br \/>\n\u6253\u5f00\u201c\u5f00\u59cb-&gt;\u6240\u6709\u7a0b\u5e8f-&gt;MySQL-&gt;MySQL Server 5.0-&gt;MySQL Command Line Client.exe\u201d\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5bc6\u7801\u5c31\u76f4\u63a5\u6309\u56de\u8f66\uff0c\u4f1a\u63d0\u793a\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\u3002<\/p>\n<p>mysql&gt; SHOW DATABASES;\/\/\u663e\u793a\u6240\u6709\u7684\u6570\u636e\u5e93\uff0c\u6ce8\u610f\u4e00\u5b9a\u8981 \u6572\u201c;\u201d\u540e\u518d\u6309\u56de\u8f66<br \/>\nmysql&gt; CREATE DATABASE mydb;\/\/\u521b\u5efa\u6570\u636e\u5e93mydb<br \/>\nmysql&gt; USE mydb;\/\/\u9009\u62e9\u4f60\u6240\u521b\u5efa\u7684\u6570\u636e\u5e93mydb<br \/>\nmysql&gt; SHOW TABLES; \/\/\u663e\u793a\u6570\u636e\u5e93\u4e2d\u7684\u8868<br \/>\nmysql&gt; CREATE TABLE mytable (username VARCHAR(100), visitelist VARCHAR(200),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remark VARCHAR(200));\/\/\u521b\u5efa\u4e00\u4e2a\u8868mytable: \u7528\u6237\u540d\uff1b\u8bbf\u95ee\u5217\u8868\uff1b\u5907\u6ce8<br \/>\nmysql&gt; DESCRIBE mytable;\/\/\u663e\u793a\u8868\u7684\u7ed3\u6784<br \/>\n\u56db\u3001VC\u7f16\u7a0b<\/p>\n<p>MYSQL mysql; \/\/\u6570\u636e\u5e93\u8fde\u63a5\u53e5\u67c4<br \/>\nmysql_init (&amp;mysql);<br \/>\nif(!mysql_real_connect(&amp;mysql,\"localhost\",\"root\",NULL,\"mydb\",3306,NULL,0))&nbsp; {\/\/mydb\u4e3a\u4f60\u6240\u521b\u5efa\u7684\u6570\u636e\u5e93\uff0c3306\u4e3a\u7aef\u53e3\u53f7\uff0c\u53ef\u81ea\u884c\u8bbe\u5b9a <br \/>\nAfxMessageBox(\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\"); <br \/>\nreturn FALSE;<br \/>\n}<br \/>\n\uff081\uff09\u5b9e\u73b0\u6dfb\u52a0 \u529f\u80fd<\/p>\n<p>CString strUsername,strList,strRemark,strSQL;<br \/>\nstrSQL.Format(\"insert into mytable(username,visitelist,remark) values(\\'%s\\',\\'%s\\',\\'%s\\')\",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strUsername,strList,strRemark);\/\/\u6ce8\u610f\u4e00\u5b9a\u8981\u5199\u5728\u4e00\u884c\uff0c\u800c\u4e14\u5fc5\u987b\u8981\u6709\\'\\'<br \/>\nif(mysql_real_query(&amp;mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength())!=0){ <br \/>\nAfxMessageBox(\"\u589e\u6dfb\u5931\u8d25\"); <br \/>\n}<\/p>\n<p>\uff082\uff09\u5b9e\u73b0\u4fee\u6539\u529f\u80fd<br \/>\nCString strUsername,strList,strRemark,strSQL,str_PreName;\/\/str_PreName\u7528\u4e8e\u8bb0\u5f55\u60f3\u8981\u4fee\u6539\u7684\u884c\uff0c\u8be6\u60c5\u8bf7\u770b\u6e90\u4ee3\u7801<br \/>\nstrSQL.Format(\"update mytable set username=\\'%s\\',visitelist=\\'%s\\',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remark=\\'%s\\' where username=\\'%s\\'\",strUsername,strList,strRemark,str_PreName);<br \/>\nif(mysql_real_query(&amp;mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength())!=0){ <br \/>\nAfxMessageBox(\"\u4fee\u6539\u5931\u8d25\"); <br \/>\n}<br \/>\n\uff083\uff09\u5b9e\u73b0\u5220\u9664\u529f\u80fd<\/p>\n<p>CString strSQL;<br \/>\nstrSQL.Format(\"delete from mytable where username=\\'%s\\'\",str_PreName);\/\/\u5fc5\u987b\u8981\u6709\\'\\'<br \/>\nif(mysql_real_query(&amp;mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength())!=0){ <br \/>\nAfxMessageBox(\"\u5220\u9664\u5931\u8d25\"); <br \/>\n}<br \/>\n\uff084\uff09\u8bfb\u53d6\u8868\u683c\u5185\u5bb9\u5230CListCtrl\u63a7\u4ef6m_list<\/p>\n<p>m_list.DeleteAllItems();<br \/>\nchar *ch_query;<br \/>\nch_query=\"select * from mytable\";<br \/>\nif(mysql_real_query(&amp;mysql,ch_query,(UINT)strlen(ch_query))!=0){ <br \/>\nAfxMessageBox(\"\u6570\u636e\u5e93\u4e2d\u8868\u683c\u51fa\u9519\"); <br \/>\n}<br \/>\nCString str;<br \/>\nMYSQL_RES *result;<br \/>\nMYSQL_ROW row;<br \/>\nif(!(result=mysql_use_result(&amp;mysql))){ <br \/>\nAfxMessageBox(\"\u8bfb\u53d6\u6570\u636e\u96c6\u5931\u8d25\"); <br \/>\n}<br \/>\nint i=0;<br \/>\nwhile(row=mysql_fetch_row(result)){<br \/>\nstr.Format(\"%s\",row[0]);<br \/>\nm_list.InsertItem(i,str);<br \/>\nstr.Format(\"%s\",row[1]);<br \/>\nm_list.SetItemText(i,1,str);<br \/>\nstr.Format(\"%s\",row[2]);<br \/>\nm_list.SetItemText(i,2,str);<br \/>\ni++;<br \/>\n}<br \/>\nmysql_free_result(result);<br \/>\n\uff085\uff09\u5173\u95ed\u6570\u636e\u5e93<\/p>\n<p>mysql_close(&amp;mysql);\/\/\u6700\u597d\u5199\u5230OnDestroy()\u51fd\u6570\u4e2d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001MySQL\u7684\u5b89\u88c5 \u6ce8\u610f\u9009\u62e9\u201c\u5b8c\u5168\u5b89\u88c5\u201d\uff08\u53ea\u6709\u8fd9\u6837\u624d\u4f1a\u5b89\u88c5VC\u7f16\u8bd1\u65f6\u9700\u8981\u7684\u5934\u6587\u4ef6\u7b49\uff09\u3002\u5b89\u88c5\u540e\u671f\u4f1a\u8fdb\u884c\u670d\u52a1\u5668\u914d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[35,131],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-Technical_literature","tag-mysql","tag-131"],"views":1376,"_links":{"self":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}