{"id":38579,"date":"2023-07-16T11:52:30","date_gmt":"2023-07-16T03:52:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=38579"},"modified":"2023-07-31T00:43:50","modified_gmt":"2023-07-30T16:43:50","slug":"%e6%80%8e%e6%a0%b7%e7%94%a8c%e7%bc%96%e7%a8%8b%e8%ae%bf%e9%97%ae%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/38579.html","title":{"rendered":"\u600e\u6837\u7528C++\u7f16\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>C++\u7f16\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93\u4e3b\u8981\u4f9d\u8d56\u4e8e\u5404\u79cd\u6570\u636e\u5e93API\uff0c\u5982ODBC\u548cMySQL Connector\u7b49\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u5957\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u5141\u8bb8C++\u7a0b\u5e8f\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5904\u7406\u7ed3\u679c\u96c6\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5MySQL Connector\/C++\u5e93\uff0c\u8be5\u5e93\u4e3aC++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u5141\u8bb8C++\u7a0b\u5e8f\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u5728MySQL\u7684\u5b98\u7f51\u4e0b\u8f7d\u5e76\u6309\u7167\u8bf4\u660e\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-38580\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/07\/\u6dfb\u52a0\u6807\u9898-29-4.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/07\/\u6dfb\u52a0\u6807\u9898-29-4.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/07\/\u6dfb\u52a0\u6807\u9898-29-4-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<p>\u5728C++\u7f16\u7a0b\u4e2d\uff0c\u8bbf\u95ee\u6570\u636e\u5e93\u9700\u8981\u501f\u52a9\u4e8e\u5404\u79cd\u6570\u636e\u5e93API\uff08Application Programming Interface\uff09\uff0c\u4f8b\u5982\uff0cODBC\uff08Open Database Connectivity\uff09\u6216\u8005\u7279\u5b9a\u6570\u636e\u5e93\u7684API\uff0c\u5982MySQL Connector\u7b49\u3002\u8fd9\u4e9bAPI\u63d0\u4f9b\u4e86\u4e00\u5957\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u5141\u8bb8C++\u7a0b\u5e8f\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u6267\u884cSQL\u8bed\u53e5\uff0c\u5e76\u5904\u7406\u7ed3\u679c\u96c6\u3002\u4e0b\u9762\u6211\u4eec\u4ee5MySQL\u4e3a\u4f8b\uff0c\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u5728C++\u7a0b\u5e8f\u4e2d\u8bbf\u95eeMySQL\u6570\u636e\u5e93\u3002<\/p>\n<h2 class=\"wp-block-heading\"><strong>1. <\/strong><strong>\u5b89\u88c5\u548c\u914d\u7f6eMySQL Connector<\/strong><\/h2>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5MySQL Connector\/C++\u5e93\uff0c\u8be5\u5e93\u4e3aC++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u5141\u8bb8C++\u7a0b\u5e8f\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u5728MySQL\u7684\u5b98\u7f51\u4e0b\u8f7d\u5e76\u6309\u7167\u8bf4\u660e\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<h2 class=\"wp-block-heading\"><strong>2. <\/strong><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>\u5b89\u88c5\u5e76\u914d\u7f6e\u597dMySQL Connector\u540e\uff0c\u63a5\u4e0b\u6765\u7684\u540d\u5217\u524d\u8305\u6b65\u662f\u5efa\u7acb\u5230\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002\u521b\u5efa\u4e00\u4e2asql::mysql::MySQL_Driver\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5176connect()\u65b9\u6cd5\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u5730\u5740\uff0c\u7528\u6237\u540d\uff0c\u5bc6\u7801\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u201c`cpp<\/p>\n<p>sql::mysql::MySQL_Driver *driver;<\/p>\n<p>sql::Connection *con;<\/p>\n<p>driver = sql::mysql::get_mysql_driver_instance();<\/p>\n<p>con = driver-&gt;connect(\u201ctcp:\/\/127.0.0.1:3306\u201d, \u201cuser\u201d, \u201cpassword\u201d);<\/p>\n<p>\u201c`<\/p>\n<h2 class=\"wp-block-heading\"><strong>3. <\/strong><strong>\u6267\u884cSQL\u8bed\u53e5<\/strong><\/h2>\n<p>\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2asql::Statement\u5bf9\u8c61\u6765\u6267\u884cSQL\u8bed\u53e5\u3002<\/p>\n<p>\u201c`cpp<\/p>\n<p>sql::Statement *stmt;<\/p>\n<p>stmt = con-&gt;createStatement();<\/p>\n<p>stmt-&gt;execute(\u201cUSE DATABASE\u201d);<\/p>\n<p>\u201c`<\/p>\n<h2 class=\"wp-block-heading\"><strong>4. <\/strong><strong>\u5904\u7406\u7ed3\u679c\u96c6<\/strong><\/h2>\n<p>\u5982\u679cSQL\u8bed\u53e5\u662f\u4e00\u4e2a\u67e5\u8be2\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2asql::ResultSet\u5bf9\u8c61\u6765\u83b7\u53d6\u548c\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<p>\u201c`cpp<\/p>\n<p>sql::ResultSet *res;<\/p>\n<p>res = stmt-&gt;executeQuery(\u201cSELECT \u2018Hello World!\u2019 AS _message\u201d);<\/p>\n<p>while (res-&gt;next()) {<\/p>\n<p>\u00a0 cout &lt;&lt; \u201c\\t\u2026 MySQL replies: \u201c;<\/p>\n<p>\u00a0 cout &lt;&lt; res-&gt;getString(\u201c_message\u201d) &lt;&lt; endl;<\/p>\n<p>\u00a0 cout &lt;&lt; \u201c\\t\u2026 MySQL says it again: \u201c;<\/p>\n<p>\u00a0 cout &lt;&lt; res-&gt;getString(1) &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>\u201c`<\/p>\n<h2 class=\"wp-block-heading\"><strong>5. <\/strong><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u540e\uff0c\u4f7f\u7528delete\u5173\u952e\u5b57\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p>\u201c`cpp<\/p>\n<p>delete res;<\/p>\n<p>delete stmt;<\/p>\n<p>delete con;<\/p>\n<p>\u201c`<\/p>\n<p>\u4ee5\u4e0a\uff0c\u4fbf\u662f\u4e00\u4e2a\u57fa\u7840\u7684C++\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u4f8b\u5b50\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4f8b\u5982\u4e8b\u52a1\u5904\u7406\u3001\u9884\u7f16\u8bd1SQL\u8bed\u53e5\u7b49\uff0c\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u901a\u8fc7\u76f8\u5e94\u7684API\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h2 class=\"wp-block-heading\"><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong><\/h2>\n<p>\u9664\u4e86MySQL\u5916\uff0c\u5176\u4ed6\u6570\u636e\u5e93\uff08\u5982PostgreSQL\u3001SQLite\u7b49\uff09\u4e5f\u90fd\u63d0\u4f9b\u4e86C++\u63a5\u53e3\uff0c\u8bbf\u95ee\u65b9\u5f0f\u4e0eMySQL\u7c7b\u4f3c\uff0c\u4e3b\u8981\u662f\u521b\u5efa\u8fde\u63a5\uff0c\u6267\u884cSQL\u8bed\u53e5\uff0c\u5904\u7406\u7ed3\u679c\u96c6\u7b49\u6b65\u9aa4\u3002\u53ea\u662f\u5728\u5177\u4f53\u7684API\u548c\u4f7f\u7528\u65b9\u6cd5\u4e0a\u6709\u6240\u4e0d\u540c\uff0c\u9700\u8981\u53c2\u8003\u5404\u81ea\u7684\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C++\u7f16\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93\u4e3b\u8981\u4f9d\u8d56\u4e8e\u5404\u79cd\u6570\u636e\u5e93API\uff0c\u5982ODBC\u548cMySQL Connector\u7b49\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u5957\u51fd [&hellip;]","protected":false},"author":1,"featured_media":38580,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/38579"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=38579"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/38579\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/38580"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=38579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=38579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=38579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}