{"id":15370,"date":"2023-12-04T19:40:14","date_gmt":"2023-12-04T11:40:14","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=15370"},"modified":"2023-12-04T19:40:51","modified_gmt":"2023-12-04T11:40:51","slug":"linux-xia-c-cao-zuo-mysql","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/linux-xia-c-cao-zuo-mysql\/","title":{"rendered":"Linux\u4e0b C++ \u64cd\u4f5c MySQL"},"content":{"rendered":"<h2><a id=\"%E4%B8%80%E3%80%81%E5%AE%89%E8%A3%85mysql%E7%9A%84-cc-api\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e00\u3001\u5b89\u88c5 MySQL\u7684 C\/C++ API<\/h2>\n<pre><code class=\"language-plain_text\">sudo apt-get install mysql-server libmysql++-dev;\n\/\/ \u5b89\u88c5 MySQL\u7684 C\/C++ API\n\nsudo cp \/usr\/lib\/mysql\/* \/usr\/lib\/\n\/\/ \u5c06\u5934\u6587\u4ef6\u590d\u5236\u5230 C\/C++ \u7684\u5934\u6587\u4ef6\u76ee\u5f55\uff08\u8fd9\u6837\u5b50\u624d\u53ef\u4ee5\u8c03\u7528\u5934\u6587\u4ef6\uff09\n\n<\/code><\/pre>\n<p>\u5b89\u88c5\u597d API \u4e4b\u540e\uff0c\u6709\u4e86\u5934\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528\u5173\u4e8e MySQL \u64cd\u4f5c\u7684\u51fd\u6570\u63a5\u53e3 \u6765\u4f7f\u7528\u4e86\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u8981\u64cd\u4f5c MySQL\uff0c\u9700\u8981\u5148\u6253\u5f00 MySQL \u670d\u52a1\u624d\u6709\u7528<\/p>\n<h2><a id=\"%E4%BA%8C%E3%80%81%E5%B8%B8%E7%94%A8mysql-api%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8c\u3001\u5e38\u7528 MySQL API \u7684\u6570\u636e\u7c7b\u578b<\/h2>\n<h3><a id=\"2-1%E3%80%81mysql\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1\u3001MYSQL<\/h3>\n<p>\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a mysql \u5bf9\u8c61\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u786e\u5b9a\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u662f\u54ea\u4e00\u4e2a\u3002<\/p>\n<pre><code class=\"language-plain_text\">MYSQL mysql;  \n\/\/ mysql\u6807\u8bb0\u5bf9\u5e94\u67d0\u4e2a\u6570\u636e\u5e93\n\n<\/code><\/pre>\n<h3><a id=\"2-2%E3%80%81-mysql-row\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2.2\u3001<code>MYSQL_ROW<\/code><\/h3>\n<p>\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u884c\u5bf9\u8c61\uff0c\u5176\u5185\u5bb9\u542b\u6709\u4e00\u884c\u7684\u6570\u636e\u3002\uff08\u6bd4\u5982\u8bf4 SELECT \u8fd4\u56de\u7ed3\u679c\u5c31\u662f \u591a\u884c\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u51fd\u6570\u8c03\u7528\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4e00\u884c\u4e00\u884c\u83b7\u53d6\uff09<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_ROW row;  \n\/\/ \u4e00\u884c\u91cc\u9762 \u5305\u62ec\u591a\u4e2a\u5b57\u6bb5\n\/\/ row[i] \u53ef\u7528\u4e8e\u8f93\u51fa\u8be5\u884c \u5bf9\u5e94\u4e8e \u7b2ci\u4e2a\u5b57\u6bb5\uff08\u5217\uff09\u7684\u6570\u636e\n\n<\/code><\/pre>\n<h3><a id=\"2-3%E3%80%81-mysql-field\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2.3\u3001<code>MYSQL_FIELD<\/code><\/h3>\n<p>\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u5b58\u50a8\u5b57\u6bb5\u4fe1\u606f\u7684\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_FIELD *field;  \n\/\/ field-&gt;name \u5b58\u50a8\u5bf9\u5e94\u5b57\u6bb5\u540d\u79f0\uff08\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5176\u4e2d\u5e94\u8be5\u8fd8\u6709\u5176\u4ed6\u5b57\u6bb5\u4fe1\u606f\uff09\n\n<\/code><\/pre>\n<h3><a id=\"2-4%E3%80%81-mysql-res\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2.4\u3001<code>MYSQL_RES<\/code><\/h3>\n<p>\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u5e93<strong>\u68c0\u7d22\u4fe1\u606f\u7ed3\u679c\u7684\u5bf9\u8c61<\/strong>\u3002<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_RES *result;\n\n<\/code><\/pre>\n<h2><a id=\"%E4%B8%89%E3%80%81%E5%B8%B8%E7%94%A8%E7%9A%84mysql-api%E5%87%BD%E6%95%B0%E4%BB%8B%E7%BB%8D\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e09\u3001\u5e38\u7528\u7684 MySQL API \u51fd\u6570\u4ecb\u7ecd<\/h2>\n<p>\u5176\u5b9e\u5728\u64cd\u4f5cmysql\u6570\u636e\u5e93\u65f6\uff0c\u6700\u5e38\u4f7f\u7528\u7684\u51fd\u6570\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a<\/p>\n<h3><a id=\"3-1%E3%80%81-mysql-init\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.1\u3001<code>mysql_init()<\/code><\/h3>\n<p>\u7528\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a MYSQL \uff08\u7c7b\u578b\uff09\u5bf9\u8c61\uff0c\u6765\u8fde\u63a5 mysql \u670d\u52a1\u7aef\u3002<\/p>\n<pre><code class=\"language-plain_text\">MYSQL *mysql_init( MYSQL *mysql );\n\/\/ \u8fd4\u56de\u503c\uff1a\u82e5\u6210\u529f\uff0c\u8fd4\u56de\u503c \u662f MySQL \u5bf9\u8c61\uff08\u8fd9\u4e2a\u4e5f\u4fdd\u5b58\u5728 \u8f93\u5165\u53c2\u6570\u4e2d\uff09\uff1b\u82e5\u51fa\u9519\uff0c\u8fd4\u56de NULL\n\/\/ \u56e0\u4e3a \u8fd4\u56de\u503c \u548c \u8f93\u5165\u53c2\u6570 \u90fd\u8bb0\u5f55\u6709 MySQL \u5bf9\u8c61\uff0c\u6240\u4ee5\u4e00\u822c\u76f4\u63a5\u7528\u8f93\u5165\u53c2\u6570\uff08\u8fd4\u56de\u503c\u5c31\u9a8c\u8bc1\u662f\u4e0d\u662f == NULL \u5373\u53ef\uff09\n\n\/\/ \u4f8b\u5b50\uff1a\nMYSQL mysql;\nmysql_init( &amp;mysql );\n\n<\/code><\/pre>\n<h3><a id=\"3-2%E3%80%81-mysql-real-connect\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.2\u3001<code>mysql_real_connect()<\/code><\/h3>\n<p>\u7528\u4e8e\u8fde\u63a5\u6570\u636e\u5e93<\/p>\n<pre><code class=\"language-plain_text\">MYSQL *mysql_real_connect (\n    MYSQL *mysql,   \/\/ mysql_init()\u521d\u59cb\u5316\u7684MYSQL\u5bf9\u8c61\uff0c\u4f5c\u4e3a\u8f93\u5165\n    const char *host,   \/\/\u4e3b\u673a\u5730\u5740\n    const char *user,   \/\/\u7528\u6237\uff0c\u4f8b\u5982\uff1aroot\n    const char *passwd,   \/\/\u6570\u636e\u5e93\u7684\u5bc6\u7801\n    const char *db,   \/\/\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\uff0c\u4f8b\u5982\uff1astudent\n    unsigned int port,   \/\/\u7aef\u53e3\uff0c\u53ef\u586b0\n    const char *unix_socket,   \/\/\u4e00\u822c\u4e3aNULL\n    unsigned long client_flag);  \/\/\u4e00\u822c\u4e3a0\n\/\/ \u8fde\u63a5\u7684\u65f6\u5019\uff0c\u9700\u8981\u8fde\u63a5\u5230 MySQL \u670d\u52a1\u5668\uff0c\u9700\u8981 IP\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u8fde\u63a5\u5230\u54ea\u4e2a\u6570\u636e\u5e93\u7b49\u4fe1\u606f\u3002\n\/\/ \u8fd4\u56de\u503c\uff0c\u82e5\u5931\u8d25\uff0c\u8fd4\u56de NULL\n\/\/ \u5982\u679c\u6210\u529f\uff0c\u5176\u8fd4\u56de\u503c \u548c \u8f93\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e00\u6837\uff0c\u6b64\u65f6\u8fd9\u4e2a MySQL \u5bf9\u8c61\u5c31\u662f\u5bf9\u5e94\u7684 \u90a3\u4e2a\u8fde\u63a5\u4e0a \u7684 \u5e93\n\n\n\/\/ \u4f8b\u5b50\nmysql_real_connect( &amp;mysql, &quot;localhost&quot;, &quot;root&quot;, \n                    &quot;123456&quot;, &quot;student&quot;, 0, NULL, 0 );\n\n<\/code><\/pre>\n<h3><a id=\"3-3%E3%80%81-mysql-query\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.3\u3001<code>mysql_query()<\/code><\/h3>\n<p>\u7528\u4e8e\u6267\u884cmysql\u547d\u4ee4\u3002\u5176\u53c2\u6570\u5e94\u4f7f\u7528c\u98ce\u683c\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-plain_text\">mysql_query( MYSQL *mysql, char * command );\n\n\/\/ \u4f8b\u5b50\nstring command = &quot;select * from info&quot;;\nmysql_query( &amp;mysql, command.c_str() );\n\n<\/code><\/pre>\n<p>\u5176\u5b9e\u5c31\u662f\uff0c\u5f53\u6211\u4eec\u81ea\u5df1\u76f4\u63a5\u64cd\u4f5c MySQL \u7684\u65f6\u5019\uff0c\u5c31\u662f\u7528\u6765 MySQL \u7684\u6307\u4ee4\uff0c\u6bd4\u5982\u67e5\u8be2 SELECT\u3002\u90a3\u4e48\u7528 C++ \u8c03\u7528 API\uff0c\u8fd9\u91cc\u8f93\u5165\u7684 command \u76f8\u5f53\u4e8e\u5c31\u662f \u6211\u4eec\u8981\u60f3\u901a\u8fc7 C++ \u53bb\u8ba9 MySQL \u505a\u4ec0\u4e48\u884c\u4e3a\u3002<\/p>\n<blockquote><p>\n\u56e0\u4e3a\u76f4\u63a5\u51fd\u6570\u64cd\u4f5c\u8fde\u63a5\u7684\u5bf9\u8c61\uff08MYSQL \u90a3\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u662f \u5e93\uff09\u662f \u5e93\uff0c\u4f46\u662f\u5177\u4f53\u6570\u636e\u662f\u5728 \u8868\uff0c\u56e0\u6b64\u4e00\u822c\u5148 \u5f97\u5230\u8fd9\u4e2a \u5e93\u7684\u8868\uff08\u6216\u8005\u5df2\u7ecf\u77e5\u9053\u662f\u54ea\u4e2a\u8868\u4e86\uff0c\u8981\u5148\u5f97\u5230\uff0c\u53ef\u4ee5\u5148 show tables \u7684 MySQL \u6307\u4ee4\uff0c\u7136\u540e\u901a\u8fc7\u83b7\u53d6\u5176\u4e2d\u4e00\u4e2a\uff0c\u5c31\u662f\u4f5c\u4e3a\u4e86 \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u8981\u5904\u7406\u7684 \u8868\u540d\uff09\uff0c\u7136\u540e\u5177\u4f53\u518d\u64cd\u4f5c\u8fd9\u4e2a\u8868\uff0c\u6bd4\u5982\u8981\u67e5\u8be2\u8868\u4e2d\u4fe1\u606f\uff0c\u5982 SELECT * from \u8868\u540d \u4f5c\u4e3a \u8fd9\u4e2a\u51fd\u6570\u7684 \u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u7684\n<\/p><\/blockquote>\n<h3><a id=\"3-4%E3%80%81-mysql-store-result\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.4\u3001<code>mysql_store_result()<\/code><\/h3>\n<p>\u7528\u4e8e\u83b7\u53d6mysql\u64cd\u4f5c\u7684\u68c0\u7d22\u7ed3\u679c\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\uff0c\u5148\u901a\u8fc7 <code>mysql_query()<\/code> \u83b7\u53d6\u68c0\u7d22\u5bf9\u8c61\uff08<code>MYSQL *mysql<\/code>\uff09\uff0c\u7136\u540e\u518d\u5229\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u5904\u7406\uff0c\u4ece\u800c\u83b7\u53d6 \u68c0\u7d22\u5bf9\u8c61\u4e2d \u5b9e\u9645\u7684\u6570\u636e\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_RES *mysql_store_result(MYSQL *mysql);\n\/\/ \u8fd4\u56de\u503c\uff1a\u82e5\u6210\u529f\uff0c\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u5c31\u662f\u5b58\u50a8\u6570\u636e\u5e93\u68c0\u7d22\u4fe1\u606f\u7ed3\u679c\u7684\u5bf9\u8c61\uff1b\u82e5\u51fa\u9519\uff0c\u8fd4\u56de NULL\n\n\/\/ \u4f8b\u5b50\nMYSQL_RES *result;\nresult = mysql_store_result( &amp;mysql );\n\n<\/code><\/pre>\n<h3><a id=\"3-5%E3%80%81-mysql-num-rows\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.5\u3001<code>mysql_num_rows()<\/code><\/h3>\n<p>\u7528\u4e8e\u83b7\u53d6\u7ed3\u679c\u96c6\u7684\u884c\u6570\u3002\uff08\u4e5f\u5c31\u662f\u68c0\u7d22\u7ed3\u679c\u4e2d\u7684\u6570\u636e\uff0c\u6709\u591a\u5c11\u884c\uff09<\/p>\n<pre><code class=\"language-plain_text\">int mysql_num_rows( MYSQL_RES *result );\n\/\/ \u8fd4\u56de\u503c\uff0c\u68c0\u7d22\u7ed3\u679c\u7684\u884c\u6570\u3002\uff08\u65e0\u7ed3\u679c\u90a3\u5c31\u662f 0\uff09\n\n<\/code><\/pre>\n<h3><a id=\"3-6%E3%80%81-mysql-num-fields\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.6\u3001<code>mysql_num_fields()<\/code><\/h3>\n<p>\u7528\u4e8e\u83b7\u53d6\u7ed3\u679c\u96c6\u7684\u5b57\u6bb5\u6570\u3002\uff08\u4e5f\u5c31\u662f\u6709\u591a\u5c11\u5217\uff0c\u6709\u51e0\u4e2a\u5b57\u6bb5\uff09<\/p>\n<pre><code class=\"language-plain_text\">int mysql_num_fields( MYSQL_RES *result );\n\/\/ \u8fd4\u56de\u503c\uff0c\u5b57\u6bb5\u7684\u4e2a\u6570\u3002\n\n\/\/example\nint fieldcount;\nfieldcount = mysql_num_fields( result );\n\n<\/code><\/pre>\n<h3><a id=\"3-7%E3%80%81-mysql-fetch-field\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.7\u3001<code>mysql_fetch_field()<\/code><\/h3>\n<p>\u7528\u4e8e\u83b7\u53d6\u7ed3\u679c\u96c6\u4e2d\uff0c\u4e0b\u4e00\u4e2a\u5b57\u6bb5\u7684\u4fe1\u606f\uff08\u5bf9\u8c61\u4fdd\u5b58\u7740\u5b57\u6bb5\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec\u6709 \u5b57\u6bb5\u540d\uff0c\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u7b49\uff09<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_FIELD* mysql_fetch_field(MYSQL_RES *result);\n\/\/ \u8fd4\u56de\u503c\uff1a\u8fd4\u56de\u7ed3\u679c\u96c6\u4e2d\u4e0b\u4e00\u4e2a\u5b57\u6bb5\u4fe1\u606f\u3002\u5982\u679c\u7ed3\u675f\u4e86\uff0c\u5c31\u662f\u5b57\u6bb5\u4fe1\u606f\u90fd\u83b7\u53d6\u5b8c\u4e86\uff0c\u90a3\u5c31\u662f NULL\n\n\/\/ \u83b7\u53d6\u5b57\u6bb5\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u5c31\u662f\u7528\u4e00\u4e2a\u5faa\u73af\uff0c\u76f4\u5230\u4e3a NULL\n\n<\/code><\/pre>\n<h3><a id=\"3-8%E3%80%81-mysql-fetch-row\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.8\u3001<code>mysql_fetch_row()<\/code><\/h3>\n<p>\u4ece\u7ed3\u679c\u96c6\u4e2d\u83b7\u53d6\u4e0b\u4e00\u884c\uff0c\u7ed3\u675f\u8fd4\u56deNULL\u3002\uff08\u4e00\u884c\u4e00\u884c\u83b7\u53d6\u7ed3\u679c\u96c6\u4e2d\u7684\u6570\u636e\uff09<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_ROW mysql_fetch_row(MYSQL_RES *result); \n\n\/\/ \u4f8b\u5b50\n\/\/ \u5176\u4e2d num \u662f\u524d\u9762\uff0c\u7edf\u8ba1\u7740\u6709\u51e0\u4e2a\u5b57\u6bb5\u3002\u4e00\u884c\u7684\u6570\u636e\u5c31\u662f \u5305\u542b\u7740 \u8fd9\u4e48\u591a\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\n\/\/ \u5148\u901a\u8fc7\u83b7\u53d6\u4e00\u6574\u884c row\uff0c\u7136\u540e \u518d\u83b7\u53d6\u8fd9\u4e00\u884c\u4e2d\uff0c\u5bf9\u5e94\u6bcf\u4e2a\u5b57\u6bb5\u7684\u5177\u4f53\u4fe1\u606f\uff0c\nMYSQL_ROW row;\nwhile( (row = mysql_fetch_row( result )) != NULL ) {\n    for( int i = 0; i &lt; num; i++ ) {\n        cout &lt;&lt; row[i] &lt;&lt; &quot;\\t\\t&quot;;\n    }\n    cout &lt;&lt; endl;\n}\n\n<\/code><\/pre>\n<h3><a id=\"3-9%E3%80%81-mysql-fetch-field-direct\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.9\u3001<code>mysql_fetch_field_direct()<\/code><\/h3>\n<p>\u7ed9\u5b9a\u5b57\u6bb5\u5e8f\u53f7\uff0c\u8fd4\u56de\u5b57\u6bb5\u7c7b\u578b\uff0c\u7ed3\u675f\u8fd4\u56deNULL\u3002\uff08\u83b7\u53d6\u7b2c i \u4e2a\u5b57\u6bb5\u7684\u5b57\u6bb5\u4fe1\u606f\uff09<\/p>\n<pre><code class=\"language-plain_text\">MYSQL_FIELD* mysql_fetch_field_direct(MYSQL_RES *result, int i);\n\/\/ \u8fd4\u56de\u503c\uff0c\u662f \u5b57\u6bb5\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u7740\u5b57\u6bb5\u540d\uff0c\u5b57\u6bb5\u6570\u636e\u7c7b\u578b\u7b49\u7b49\n\n\/\/example\nint num = mysql_num_fields( result );  \/\/\u8fd4\u56de\u5b57\u6bb5\u4e2a\u6570\nfor( int i = 0; i &lt; num; i++ ) {\n    field = mysql_fetch_field_direct( result, i );  \/\/\u8fd4\u56de\u5b57\u6bb5\u7c7b\u578b\n    cout &lt;&lt; field-&gt;name &lt;&lt; &quot;\\t\\t&quot;;  \/\/\u8f93\u51fa\u5b57\u6bb5\u540d\n}\ncout &lt;&lt; endl;\n\n<\/code><\/pre>\n<h3><a id=\"3-10%E3%80%81-mysql-close\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.10\u3001<code>mysql_close()<\/code><\/h3>\n<p>\u7528\u4e8e\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p>\u8fde\u63a5\u5b8c\uff0c\u5904\u7406\u5b8c\uff0c\u5c31\u5173\u95ed MySQL \u8fde\u63a5\uff08\u548c\u5904\u7406\u5b8c\u6587\u4ef6\u4fe1\u606f\u540e\uff0c\u5173\u95ed\u6587\u4ef6\u6d41\u4e00\u6837\uff09<\/p>\n<pre><code class=\"language-plain_text\">mysql_close( MYSQL *mysql );\n\n<\/code><\/pre>\n<h2><a id=\"%E5%9B%9B%E3%80%81%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u56db\u3001\u793a\u4f8b\u4ee3\u7801<\/h2>\n<pre><code class=\"language-plain_text\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;mysql\/mysql.h&gt;\nusing namespace std;\n\n\/\/ \u901a\u8fc7\u5efa\u7acb\u4e00\u4e2a \u64cd\u4f5c\u6570\u636e\u5e93 \u7684\u7c7b\uff0c\u540e\u9762\u5c31\u6bcf\u64cd\u4f5c\u5177\u4f53\u7684\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u6211\u4eec\u5c31\u5b9e\u4f8b\u5316\u4e00\u4e2a \u5bf9\u8c61\uff0c\u8fd9\u6837\u5b50\u5c31\u5f88\u65b9\u4fbf\u4e86\n\/\/ \u7c7b \u53ef\u4ee5\u5199\u5728 \u53e6\u4e00\u4e2a .h \u5934\u6587\u4ef6\uff0c\u5b9e\u73b0\u5728\u53e6\u4e00\u4e2a .cpp \u4e2d\uff0c\u7136\u540e\u589e\u52a0\u5230\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\nclass MysqlDB {\nprivate:\n    \/\/ \u8981\u7528\u5230\u7684\u51e0\u4e2a\u6570\u636e\u7c7b\u578b\n    MYSQL mysql;     \/\/ \u64cd\u4f5c\u7684 \u6570\u636e\u5e93\u5bf9\u8c61\n    MYSQL_ROW row;   \/\/ \u83b7\u53d6\u4e00\u884c\u7684\u6570\u636e\u4fe1\u606f\n    MYSQL_RES *result;  \/\/ \u68c0\u7d22\u7684\u7ed3\u679c\u96c6\n    MYSQL_FIELD *field;  \/\/ \u5b57\u6bb5\u7684\u4fe1\u606f\uff0c\u5982 \u5b57\u6bb5\u540d\u3001\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u7b49\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u5bf9\u8c61\n    MysqlDB() {\n        if( mysql_init( &amp;mysql ) == NULL ) {\n            cout &lt;&lt; &quot;init error, line: &quot; &lt;&lt; __LINE__ &lt;&lt; endl;\n            exit(-1);\n        }\n    }\n    \/\/ \u6790\u6784\u51fd\u6570\uff0c\u90a3\u5c31\u7528\u4e8e\uff0c\u5173\u95ed MySQL \u8fde\u63a5\n    ~MysqlDB() {\n        mysql_close( &amp;mysql );\n    }\n    \n    \/\/ \u6210\u5458\u51fd\u6570\uff1a\u7528\u4e8e\u8fde\u63a5 \u5230\u6570\u636e\u5e93 \u4e2d\uff0c\u91cd\u8981\u7684\u51e0\u4e2a\u53c2\u6570\uff08\u5b9a\u4e49\u662f string\uff0c\u4f46\u662f\u8981\u8f6c\u5316\u5230 C \u7684 str\uff09\n    void connect( string host, string user, string passwd,  string database ) {\n        if( !mysql_real_connect( &amp;mysql, host.c_str(), user.c_str(), passwd.c_str(), \n                                    database.c_str(), 0, NULL, 0 ) ) {\n            cout &lt;&lt; &quot;connect error, line: &quot; &lt;&lt; __LINE__ &lt;&lt; endl;\n            exit(-1);\n        }\n    }\n    \/\/ \u8fd8\u53ef\u4ee5\u7ee7\u7eed\u5b9a\u4e49\u5176\u4ed6\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u5b9e\u73b0 \u66f4\u591a\u7684\u529f\u80fd\n    void add();\n    void print();\n};\n\n\/\/ \u7ed9 \u8868 info \u63d2\u5165\u8f93\u5165\u7684\u4fe1\u606f\n\/\/ \u8fd9\u91cc\u8868\u540d\uff0c\u9ed8\u8ba4\u77e5\u9053\uff0c\u5982\u679c\u4e0d\u77e5\u9053\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u901a\u8fc7 \u5b9a\u4e49\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u5148\u83b7\u53d6 \u5e93\u4e2d\uff0c\u6240\u6709\u7684\u8868\n\/\/ \u8fd9\u6837\u5b50\uff0c\u8868\u540d \u4e5f\u53ef\u4ee5\u5b9a\u4e49\u6210\u4e00\u4e2a string\uff0c\u7528\u4e8e\u6211\u4eec\u7684\u5177\u4f53\u9009\u62e9\nvoid MysqlDB::add() {\n    string id, name, sex, birthday;\n    do {\n        cout &lt;&lt; &quot;\u8bf7\u8f93\u5165\u5b66\u751f\u4fe1\u606f:\\n&quot;;\n\n        cin &gt;&gt; id &gt;&gt; name &gt;&gt; sex &gt;&gt; birthday;\n        string sql = &quot;INSERT INTO info VALUES('&quot; + id + &quot;', '&quot; + name + \n                        &quot;', '&quot; + sex + &quot;', '&quot; + birthday + &quot;');&quot;;\n\n        mysql_query( &amp;mysql, sql.c_str() );\n        cout &lt;&lt; &quot;\u662f\u5426\u7ee7\u7eed\uff08y\/n\uff09: &quot;;\n        cin &gt;&gt; id;\n    } while( id == &quot;y&quot; );\n}\n\n\/\/ \u5c06\u67e5\u8be2\u5230\u7684\u7ed3\u679c\uff0c\u6253\u5370\u663e\u793a\u51fa\u6765\nvoid MysqlDB::print() {\n\n    \/\/ string sql = &quot;select * from info where name = '&quot; + name + &quot;';&quot;;  \/\/\u8981\u6709''\n    string sql = &quot;select * from info;&quot;;\n    mysql_query( &amp;mysql, sql.c_str() );\n\n    result = mysql_store_result( &amp;mysql );\n    if( !result ) {\n        cout &lt;&lt; &quot;result error, line : &quot; &lt;&lt; __LINE__ &lt;&lt; endl;\n        return ;\n    }\n\n    int num;\n    num = mysql_num_fields( result );  \/\/\u8fd4\u56de\u5b57\u6bb5\u4e2a\u6570\n    for( int i = 0; i &lt; num; i++ ) {\n        field = mysql_fetch_field_direct( result, i );  \/\/\u8fd4\u56de\u5b57\u6bb5\u7c7b\u578b\n        cout &lt;&lt; field-&gt;name &lt;&lt; &quot;\\t\\t&quot;;  \/\/\u8f93\u51fa\u5b57\u6bb5\u540d\n    }\n    cout &lt;&lt; endl;\n\n    while( row = mysql_fetch_row( result ), row ) {\n        for( int i = 0; i &lt; num; i++ ) {\n            cout &lt;&lt; row[i] &lt;&lt; &quot;\\t\\t&quot;;\n        }\n        cout &lt;&lt; endl;\n    }\n}\n\n\/\/ main \u51fd\u6570\u4e2d\nint main() {\n    \/\/ \u5b9a\u4e49\u6210\u7c7b\u4e4b\u540e\uff0c\u8981\u64cd\u4f5c\u4e00\u4e2a \u5e93\uff0c\u5c31\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u5373\u53ef\uff0c\u800c\u4e14\u51fd\u6570\u529f\u80fd\u4e5f\u5c01\u88c5\u597d\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528\u5c31\u53ef\u4ee5\n\n    MysqlDB db;   \/\/ \u5b9e\u4f8b\u5316\u6210\u5bf9\u8c61\n    \n    \/\/ \u7136\u540e\u8fde\u63a5\n    db.connect( &quot;localhost&quot;, &quot;root&quot;, &quot;niliushall&quot;, &quot;student&quot; ); \n    \n    \/\/ \u5148\u6253\u5370\uff0c\u589e\u52a0\u6570\u636e\uff0c\u518d\u6253\u5370\n    db.print();\n    db.add();\n    db.print();\n\n    return 0;\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5b89\u88c5 MySQL\u7684 C\/C++ API sudo apt-get install mysql-server [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-15370","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=15370"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15370\/revisions"}],"predecessor-version":[{"id":15371,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/15370\/revisions\/15371"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=15370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=15370"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=15370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}