{"id":172415,"date":"2024-11-07T12:56:21","date_gmt":"2024-11-07T04:56:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/172415\/"},"modified":"2024-11-07T12:56:22","modified_gmt":"2024-11-07T04:56:22","slug":"vc-%e9%80%a3-mysql-%e4%b8%ad%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%ad%98%e9%bb%9e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/172415\/","title":{"rendered":"VC++ \u9023 MySQL \u4e2d\u7684\u4e00\u4e9b\u5c0f\u77e5\u8b58\u9ede"},"content":{"rendered":"<h1 id=\"vc-%e9%80%a3-mysql-%e4%b8%ad%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%ad%98%e9%bb%9e-BkBvQuEhDC\">VC++ \u9023 MySQL \u4e2d\u7684\u4e00\u4e9b\u5c0f\u77e5\u8b58\u9ede<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0cC++ \u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u800c MySQL \u5247\u662f\u6700\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u4e4b\u4e00\u3002\u5c07 VC++ \u8207 MySQL \u7d50\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5275\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u80fd\u5920\u8f15\u9b06\u5730\u7ba1\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u4e9b\u5728 VC++ \u4e2d\u9023\u63a5 MySQL \u7684\u5c0f\u77e5\u8b58\u9ede\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"1-%e7%92%b0%e5%a2%83%e8%a8%ad%e7%bd%ae-BkBvQuEhDC\">1. \u74b0\u5883\u8a2d\u7f6e<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86 MySQL \u6578\u64da\u5eab\u548c VC++ \u958b\u767c\u74b0\u5883\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u4e0b\u8f09 MySQL Connector\/C++\uff0c\u9019\u662f\u4e00\u500b\u7528\u65bc C++ \u7684 MySQL \u5ba2\u6236\u7aef\u5eab\uff0c\u80fd\u5920\u5e6b\u52a9\u60a8\u8207 MySQL \u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-mysql-connector-c-BkBvQuEhDC\">\u5b89\u88dd MySQL Connector\/C++<\/h3>\n<ul>\n<li>\u8a2a\u554f MySQL \u5b98\u65b9\u7db2\u7ad9\uff0c\u4e0b\u8f09\u9069\u5408\u60a8\u7cfb\u7d71\u7684 Connector\/C++\u3002<\/li>\n<li>\u6309\u7167\u5b89\u88dd\u6307\u793a\u9032\u884c\u5b89\u88dd\uff0c\u4e26\u78ba\u4fdd\u5c07\u5176\u8def\u5f91\u6dfb\u52a0\u5230\u7cfb\u7d71\u74b0\u5883\u8b8a\u91cf\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"2-%e9%80%a3%e6%8e%a5-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-BkBvQuEhDC\">2. \u9023\u63a5 MySQL \u6578\u64da\u5eab<\/h2>\n<p>\u5728 VC++ \u4e2d\u9023\u63a5 MySQL \u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code>\n#include &lt;mysql_driver.h&gt;\n#include &lt;mysql_connection.h&gt;\n\nint main() {\n    sql::mysql::MySQL_Driver *driver;\n    sql::Connection *con;\n\n    \/\/ \u5275\u5efa MySQL \u9a45\u52d5\n    driver = sql::mysql::get_mysql_driver_instance();\n    \n    \/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\n    con = driver-&gt;connect(\"tcp:\/\/127.0.0.1:3306\", \"user\", \"password\");\n    \n    \/\/ \u9078\u64c7\u6578\u64da\u5eab\n    con-&gt;setSchema(\"testdb\");\n\n    \/\/ \u9032\u884c\u5176\u4ed6\u64cd\u4f5c...\n\n    delete con;\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u60a8\u9700\u8981\u5c07 &#8220;user&#8221; \u548c &#8220;password&#8221; \u66ff\u63db\u70ba\u60a8\u7684 MySQL \u7528\u6236\u540d\u548c\u5bc6\u78bc\uff0c\u4e26\u5c07 &#8220;testdb&#8221; \u66ff\u63db\u70ba\u60a8\u8981\u4f7f\u7528\u7684\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"3-%e5%9f%b7%e8%a1%8c-sql-%e6%9f%a5%e8%a9%a2-BkBvQuEhDC\">3. \u57f7\u884c SQL \u67e5\u8a62<\/h2>\n<p>\u9023\u63a5\u5230\u6578\u64da\u5eab\u5f8c\uff0c\u60a8\u53ef\u4ee5\u57f7\u884c SQL \u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u57f7\u884c\u67e5\u8a62\u4e26\u7372\u53d6\u7d50\u679c\uff1a<\/p>\n<pre><code>\nsql::Statement *stmt;\nsql::ResultSet *res;\n\nstmt = con-&gt;createStatement();\nres = stmt-&gt;executeQuery(\"SELECT * FROM users\");\n\nwhile (res-&gt;next()) {\n    std::cout &lt;&lt; \"User ID: \" &lt;&lt; res-&gt;getInt(\"id\") &lt;&lt; \", Name: \" &lt;&lt; res-&gt;getString(\"name\") &lt;&lt; std::endl;\n}\n\ndelete res;\ndelete stmt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u8a9e\u53e5\u5c0d\u8c61\uff0c\u4e26\u57f7\u884c\u4e86\u4e00\u500b\u7c21\u55ae\u7684 SELECT \u67e5\u8a62\uff0c\u7136\u5f8c\u904d\u6b77\u7d50\u679c\u96c6\u4e26\u8f38\u51fa\u7528\u6236\u7684 ID \u548c\u540d\u7a31\u3002<\/p>\n<h2 id=\"4-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-BkBvQuEhDC\">4. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 try-catch \u584a\u4f86\u6355\u7372\u7570\u5e38\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\uff1a<\/p>\n<pre><code>\ntry {\n    \/\/ \u9023\u63a5\u548c\u67e5\u8a62\u4ee3\u78bc\n} catch (sql::SQLException &amp;e) {\n    std::cerr &lt;&lt; \"Error: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u51fa\u73fe\u932f\u8aa4\u6642\uff0c\u7a0b\u5e8f\u4e0d\u6703\u5d29\u6f70\uff0c\u4e26\u80fd\u5920\u63d0\u4f9b\u6709\u7528\u7684\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"5-%e9%87%8b%e6%94%be%e8%b3%87%e6%ba%90-BkBvQuEhDC\">5. \u91cb\u653e\u8cc7\u6e90<\/h2>\n<p>\u5728\u5b8c\u6210\u6578\u64da\u5eab\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u91cb\u653e\u8cc7\u6e90\uff0c\u4ee5\u907f\u514d\u5167\u5b58\u6cc4\u6f0f\u3002\u4f7f\u7528 delete \u91cb\u653e\u5275\u5efa\u7684\u5c0d\u8c61\u662f\u826f\u597d\u7684\u7de8\u7a0b\u7fd2\u6163\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-BkBvQuEhDC\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u7684\u4ecb\u7d39\uff0c\u6211\u5011\u4e86\u89e3\u4e86\u5982\u4f55\u5728 VC++ \u4e2d\u9023\u63a5 MySQL \u6578\u64da\u5eab\uff0c\u5305\u62ec\u74b0\u5883\u8a2d\u7f6e\u3001\u9023\u63a5\u6578\u64da\u5eab\u3001\u57f7\u884c\u67e5\u8a62\u3001\u932f\u8aa4\u8655\u7406\u4ee5\u53ca\u8cc7\u6e90\u91cb\u653e\u7b49\u91cd\u8981\u6b65\u9a5f\u3002\u9019\u4e9b\u77e5\u8b58\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7684\u6578\u64da\u9a45\u52d5\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u5de5\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u60a8\u90fd\u53ef\u4ee5\u5728\u6211\u5011\u7684\u7db2\u7ad9\u4e0a\u627e\u5230\u5408\u9069\u7684\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 VC++ \u8207 MySQL \u7684\u6574\u5408\u6280\u5de7\uff0c\u4e86\u89e3\u5e38\u898b\u554f\u984c\u53ca\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u8207\u6578\u64da\u7ba1\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-172415","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=172415"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172415\/revisions"}],"predecessor-version":[{"id":172416,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172415\/revisions\/172416"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=172415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=172415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=172415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}