{"id":60213,"date":"2024-10-13T00:59:17","date_gmt":"2024-10-12T16:59:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/60213\/"},"modified":"2024-10-13T00:59:17","modified_gmt":"2024-10-12T16:59:17","slug":"c%e8%aa%9e%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-%e5%9c%a8c%e4%b8%ad%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/60213\/","title":{"rendered":"C\u8a9e\u8a00\u4e2d\u5982\u4f55\u9023\u63a5\u6578\u64da\u5eab (\u5728c\u4e2d\u67e5\u8a62\u6578\u64da\u5eab\u9023\u63a5)"},"content":{"rendered":"<h1 id=\"c%e8%aa%9e%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-%e5%9c%a8c%e4%b8%ad%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-HSZgIeCGBJ\">C\u8a9e\u8a00\u4e2d\u5982\u4f55\u9023\u63a5\u6578\u64da\u5eab (\u5728C\u4e2d\u67e5\u8a62\u6578\u64da\u5eab\u9023\u63a5)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002C\u8a9e\u8a00\u4f5c\u70ba\u4e00\u7a2e\u5e95\u5c64\u7de8\u7a0b\u8a9e\u8a00\uff0c\u96d6\u7136\u4e0d\u5982\u4e00\u4e9b\u9ad8\u7d1a\u8a9e\u8a00\u90a3\u6a23\u76f4\u63a5\u652f\u6301\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u901a\u904e\u4e00\u4e9b\u5eab\u4f86\u5be6\u73fe\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u548c\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728C\u8a9e\u8a00\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u78bc\u4f86\u5e6b\u52a9\u7406\u89e3\u3002<\/p>\n<h2 id=\"1-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%81%b8%e6%93%87-HSZgIeCGBJ\">1. \u6578\u64da\u5eab\u9078\u64c7<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u4e00\u500b\u6578\u64da\u5eab\u3002\u5e38\u898b\u7684\u9078\u64c7\u5305\u62ec MySQL\u3001PostgreSQL \u548c SQLite\u3002\u9019\u4e9b\u6578\u64da\u5eab\u90fd\u6709\u5404\u81ea\u7684\u7279\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"2-%e5%ae%89%e8%a3%9d%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a9%85%e5%8b%95-HSZgIeCGBJ\">2. \u5b89\u88dd\u6578\u64da\u5eab\u9a45\u52d5<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684\u6578\u64da\u5eab\u9a45\u52d5\u3002\u4ee5 MySQL \u70ba\u4f8b\uff0c\u958b\u767c\u8005\u9700\u8981\u5b89\u88dd MySQL C API\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5728 Linux \u7cfb\u7d71\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt-get install libmysqlclient-dev<\/code><\/pre>\n<h2 id=\"3-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-HSZgIeCGBJ\">3. \u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5305\u542b\u5fc5\u8981\u7684\u982d\u6587\u4ef6<\/li>\n<li>\u521d\u59cb\u5316\u6578\u64da\u5eab\u9023\u63a5<\/li>\n<li>\u5efa\u7acb\u9023\u63a5<\/li>\n<li>\u57f7\u884c\u67e5\u8a62<\/li>\n<li>\u8655\u7406\u7d50\u679c<\/li>\n<li>\u95dc\u9589\u9023\u63a5<\/li>\n<\/ul>\n<h2 id=\"4-%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-HSZgIeCGBJ\">4. \u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 MySQL \u7684\u7c21\u55ae\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 C \u8a9e\u8a00\u4e2d\u9023\u63a5\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\uff1a<\/p>\n<pre><code>#include \n#include \n#include \n\nint main() {\n    MYSQL *conn;\n    MYSQL_RES *res;\n    MYSQL_ROW row;\n\n    \/\/ \u521d\u59cb\u5316 MySQL \u9023\u63a5\n    conn = mysql_init(NULL);\n    if (conn == NULL) {\n        fprintf(stderr, \"mysql_init() failedn\");\n        return EXIT_FAILURE;\n    }\n\n    \/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\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    \/\/ \u57f7\u884c\u67e5\u8a62\n    if (mysql_query(conn, \"SELECT * FROM table_name\")) {\n        fprintf(stderr, \"SELECT * FROM table_name failed. Error: %sn\", mysql_error(conn));\n        mysql_close(conn);\n        return EXIT_FAILURE;\n    }\n\n    \/\/ \u7372\u53d6\u7d50\u679c\u96c6\n    res = mysql_store_result(conn);\n    if (res == NULL) {\n        fprintf(stderr, \"mysql_store_result() failed. Error: %sn\", mysql_error(conn));\n        mysql_close(conn);\n        return EXIT_FAILURE;\n    }\n\n    \/\/ \u8f38\u51fa\u7d50\u679c\n    while ((row = mysql_fetch_row(res)) != NULL) {\n        printf(\"%sn\", row[0]); \/\/ \u5047\u8a2d\u53ea\u8f38\u51fa\u7b2c\u4e00\u5217\n    }\n\n    \/\/ \u91cb\u653e\u7d50\u679c\u96c6\n    mysql_free_result(res);\n    \/\/ \u95dc\u9589\u9023\u63a5\n    mysql_close(conn);\n    return EXIT_SUCCESS;\n}\n<\/code><\/pre>\n<h2 id=\"5-%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-HSZgIeCGBJ\">5. \u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 C \u8a9e\u8a00\u9023\u63a5\u6578\u64da\u5eab\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u6578\u64da\u5eab\u670d\u52d9\u5668\u6b63\u5728\u904b\u884c\u3002<\/li>\n<li>\u6aa2\u67e5\u7528\u6236\u540d\u548c\u5bc6\u78bc\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u6ce8\u610f SQL \u6ce8\u5165\u653b\u64ca\uff0c\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u589e\u5f37\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HSZgIeCGBJ\">\u7e3d\u7d50<\/h2>\n<p>\u5728 C \u8a9e\u8a00\u4e2d\u9023\u63a5\u6578\u64da\u5eab\u96d6\u7136\u9700\u8981\u4e00\u4e9b\u984d\u5916\u7684\u6b65\u9a5f\uff0c\u4f46\u901a\u904e\u5408\u9069\u7684\u5eab\u548c\u9a45\u52d5\uff0c\u9019\u4e00\u904e\u7a0b\u8b8a\u5f97\u76f8\u5c0d\u7c21\u55ae\u3002\u7121\u8ad6\u662f\u4f7f\u7528 MySQL\u3001PostgreSQL \u9084\u662f SQLite\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728C\u8a9e\u8a00\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u67e5\u8a62\u6578\u64da\uff0c\u4e26\u5be6\u73fe\u6578\u64da\u64cd\u4f5c\u7684\u5b8c\u6574\u6307\u5357\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-60213","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60213","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=60213"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60213\/revisions"}],"predecessor-version":[{"id":60214,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/60213\/revisions\/60214"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=60213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=60213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=60213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}