{"id":60179,"date":"2024-10-13T00:42:15","date_gmt":"2024-10-12T16:42:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/60179\/"},"modified":"2024-10-13T00:42:15","modified_gmt":"2024-10-12T16:42:15","slug":"c%e8%aa%9e%e8%a8%80%e5%af%a6%e7%8f%be%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-c%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/60179\/","title":{"rendered":"C\u8a9e\u8a00\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c (c\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c)"},"content":{"rendered":"<h1 id=\"c%e8%aa%9e%e8%a8%80%e5%af%a6%e7%8f%be%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-c%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-ldpIVHgzTP\">C\u8a9e\u8a00\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c (c\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c)<\/h1>\n<p>\u96a8\u8457\u5fae\u4fe1\u7684\u666e\u53ca\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u958b\u767c\u8005\u5e0c\u671b\u80fd\u5920\u5229\u7528C\u8a9e\u8a00\u4f86\u9032\u884c\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u4f86\u5be6\u73fe\u5c0d\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5305\u62ec\u9023\u63a5\u6578\u64da\u5eab\u3001\u57f7\u884c\u67e5\u8a62\u548c\u8655\u7406\u7d50\u679c\u7b49\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"1-%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a6%82%e8%bf%b0-ldpIVHgzTP\">1. \u5fae\u4fe1\u6578\u64da\u5eab\u6982\u8ff0<\/h2>\n<p>\u5fae\u4fe1\u7684\u6578\u64da\u5eab\u4e3b\u8981\u662f\u57fa\u65bcMySQL\u6216SQLite\u7b49\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u3002\u9019\u4e9b\u6578\u64da\u5eab\u80fd\u5920\u9ad8\u6548\u5730\u5b58\u5132\u548c\u6aa2\u7d22\u7528\u6236\u6578\u64da\u3001\u6d88\u606f\u8a18\u9304\u7b49\u4fe1\u606f\u3002\u4f7f\u7528C\u8a9e\u8a00\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u958b\u767c\u8005\u9700\u8981\u4f7f\u7528\u76f8\u61c9\u7684\u6578\u64da\u5eabAPI\u4f86\u9032\u884c\u9023\u63a5\u548c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"2-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-ldpIVHgzTP\">2. \u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u958b\u767c\u8005\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86C\u8a9e\u8a00\u7684\u7de8\u8b6f\u74b0\u5883\u4ee5\u53ca\u76f8\u61c9\u7684\u6578\u64da\u5eab\u9a45\u52d5\u3002\u4ee5MySQL\u70ba\u4f8b\uff0c\u958b\u767c\u8005\u9700\u8981\u5b89\u88ddMySQL C API\uff0c\u4e26\u5728\u7de8\u8b6f\u6642\u93c8\u63a5\u76f8\u61c9\u7684\u5eab\u6587\u4ef6\u3002<\/p>\n<h3 id=\"2-1-%e5%ae%89%e8%a3%9dmysql-c-api-ldpIVHgzTP\">2.1 \u5b89\u88ddMySQL C API<\/h3>\n<ul>\n<li>\u5728Linux\u7cfb\u7d71\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/li>\n<pre><code>sudo apt-get install libmysqlclient-dev<\/code><\/pre>\n<li>\u5728Windows\u7cfb\u7d71\u4e2d\uff0c\u5247\u9700\u8981\u5f9eMySQL\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09\u76f8\u61c9\u7684\u5b89\u88dd\u5305\u3002<\/li>\n<\/ul>\n<h2 id=\"3-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-ldpIVHgzTP\">3. \u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n#include \n#include \n#include \n\nint main() {\n    MYSQL *conn;\n    conn = mysql_init(NULL);\n    \n    if (conn == NULL) {\n        fprintf(stderr, \"mysql_init() failedn\");\n        return EXIT_FAILURE;\n    }\n\n    if (mysql_real_connect(conn, \"localhost\", \"user\", \"password\", \"database\", 0, NULL, 0) == NULL) {\n        fprintf(stderr, \"mysql_real_connect() failedn\");\n        mysql_close(conn);\n        return EXIT_FAILURE;\n    }\n\n    printf(\"Connected to database successfully!n\");\n    mysql_close(conn);\n    return EXIT_SUCCESS;\n}\n<\/code><\/pre>\n<h2 id=\"4-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-ldpIVHgzTP\">4. \u57f7\u884c\u67e5\u8a62<\/h2>\n<p>\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u57f7\u884c\u67e5\u8a62\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst char *query = \"SELECT * FROM users\";\nif (mysql_query(conn, query)) {\n    fprintf(stderr, \"SELECT * failed. Error: %sn\", mysql_error(conn));\n    return EXIT_FAILURE;\n}\n\nMYSQL_RES *result = mysql_store_result(conn);\nif (result == NULL) {\n    fprintf(stderr, \"mysql_store_result() failed. Error: %sn\", mysql_error(conn));\n    return EXIT_FAILURE;\n}\n\nint num_fields = mysql_num_fields(result);\nMYSQL_ROW row;\n\nwhile ((row = mysql_fetch_row(result))) {\n    for (int i = 0; i &lt; num_fields; i++) {\n        printf(&quot;%s &quot;, row[i] ? row[i] : &quot;NULL&quot;);\n    }\n    printf(&quot;n&quot;);\n}\n\nmysql_free_result(result);\n<\/code><\/pre>\n<h2 id=\"5-%e8%99%95%e7%90%86%e7%b5%90%e6%9e%9c-ldpIVHgzTP\">5. \u8655\u7406\u7d50\u679c<\/h2>\n<p>\u5728\u57f7\u884c\u67e5\u8a62\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u8655\u7406\u67e5\u8a62\u7d50\u679c\u3002\u4e0a\u9762\u7684\u4ee3\u78bc\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86mysql_fetch_row()\u51fd\u6578\u4f86\u9010\u884c\u7372\u53d6\u7d50\u679c\u96c6\u4e2d\u7684\u6578\u64da\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9032\u4e00\u6b65\u8655\u7406\u9019\u4e9b\u6578\u64da\u3002<\/p>\n<h2 id=\"6-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-ldpIVHgzTP\">6. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u958b\u767c\u8005\u61c9\u8a72\u6aa2\u67e5\u6bcf\u500bAPI\u8abf\u7528\u7684\u8fd4\u56de\u503c\uff0c\u4e26\u5728\u51fa\u73fe\u932f\u8aa4\u6642\u4f7f\u7528mysql_error()\u51fd\u6578\u4f86\u7372\u53d6\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ldpIVHgzTP\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528C\u8a9e\u8a00\u9032\u884c\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u4e00\u500b\u76f8\u5c0d\u76f4\u63a5\u7684\u904e\u7a0b\uff0c\u901a\u904e\u6b63\u78ba\u7684API\u8abf\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u6578\u64da\u5eab\u3001\u57f7\u884c\u67e5\u8a62\u4e26\u8655\u7406\u7d50\u679c\u3002\u96a8\u8457\u5c0d\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6df1\u5165\u7406\u89e3\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u529f\u80fd\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u60a8\u7684\u61c9\u7528\u548c\u6578\u64da\u5eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22C\u8a9e\u8a00\u5982\u4f55\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5b78\u7fd2\u6578\u64da\u5b58\u53d6\u3001\u7ba1\u7406\u6280\u5de7\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u8207\u61c9\u7528\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-60179","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60179","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=60179"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60179\/revisions"}],"predecessor-version":[{"id":60180,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60179\/revisions\/60180"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=60179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=60179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=60179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}