{"id":62353,"date":"2024-10-13T18:18:16","date_gmt":"2024-10-13T10:18:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/62353\/"},"modified":"2024-10-13T18:18:16","modified_gmt":"2024-10-13T10:18:16","slug":"lua%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%ae%9e%e7%8e%b0%e9%ab%98%e6%95%88%e8%ae%80%e5%af%ab-lua%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/62353\/","title":{"rendered":"Lua\u8f15\u9b06\u64cd\u63a7\u6578\u64da\u5eab\uff0c\u5b9e\u73b0\u9ad8\u6548\u8b80\u5beb (lua\u6578\u64da\u5eab\u8b80\u5beb)"},"content":{"rendered":"<h1 id=\"lua%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%ae%9e%e7%8e%b0%e9%ab%98%e6%95%88%e8%ae%80%e5%af%ab-lua%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab-dlNroFxJTl\">Lua\u8f15\u9b06\u64cd\u63a7\u6578\u64da\u5eab\uff0c\u5b9e\u73b0\u9ad8\u6548\u8b80\u5beb (lua\u6578\u64da\u5eab\u8b80\u5beb)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9ad8\u6548\u8b80\u5beb\u662f\u61c9\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u95dc\u9375\u56e0\u7d20\u4e4b\u4e00\u3002Lua\u4f5c\u70ba\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u9ad8\u6548\u7684\u57f7\u884c\u6027\u80fd\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Lua\u4f86\u8f15\u9b06\u64cd\u63a7\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u8b80\u5beb\u3002<\/p>\n<h2 id=\"lua%e7%9a%84%e7%89%b9%e9%bb%9e-dlNroFxJTl\">Lua\u7684\u7279\u9ede<\/h2>\n<p>Lua\u662f\u4e00\u7a2e\u5d4c\u5165\u5f0f\u8173\u672c\u8a9e\u8a00\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u7279\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u8f15\u91cf\u7d1a\uff1a<\/strong>Lua\u7684\u6838\u5fc3\u5eab\u975e\u5e38\u5c0f\uff0c\u9069\u5408\u5d4c\u5165\u5230\u5404\u7a2e\u61c9\u7528\u4e2d\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u5b78\uff1a<\/strong>Lua\u7684\u8a9e\u6cd5\u7c21\u55ae\uff0c\u6613\u65bc\u4e0a\u624b\uff0c\u9069\u5408\u5feb\u901f\u958b\u767c\u3002<\/li>\n<li><strong>\u9ad8\u6548\u6027\u80fd\uff1a<\/strong>Lua\u7684\u57f7\u884c\u901f\u5ea6\u5feb\uff0c\u80fd\u5920\u6eff\u8db3\u9ad8\u6027\u80fd\u61c9\u7528\u7684\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"lua%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%95%b4%e5%90%88-dlNroFxJTl\">Lua\u8207\u6578\u64da\u5eab\u7684\u6574\u5408<\/h2>\n<p>Lua\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u8207\u6578\u64da\u5eab\u9032\u884c\u6574\u5408\uff0c\u6700\u5e38\u898b\u7684\u65b9\u5f0f\u662f\u4f7f\u7528LuaSQL\u9019\u500b\u5eab\u3002LuaSQL\u662f\u4e00\u500b\u7528\u65bcLua\u7684\u6578\u64da\u5eab\u63a5\u53e3\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71\uff0c\u5982MySQL\u3001SQLite\u3001PostgreSQL\u7b49\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dluasql-dlNroFxJTl\">\u5b89\u88ddLuaSQL<\/h3>\n<p>\u5728\u4f7f\u7528LuaSQL\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88dd\u5b83\u3002\u4ee5\u4e0b\u662f\u5b89\u88ddLuaSQL\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>luarocks install luasql-mysql<\/code><\/pre>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-dlNroFxJTl\">\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528LuaSQL\u9032\u884c\u57fa\u672c\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<h4 id=\"%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-dlNroFxJTl\">\u9023\u63a5\u6578\u64da\u5eab<\/h4>\n<pre><code>local luasql = require \"luasql.mysql\"\nlocal env = luasql.mysql()\nlocal conn = env:connect(\"database_name\", \"username\", \"password\", \"host\", port)<\/code><\/pre>\n<h4 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-dlNroFxJTl\">\u67e5\u8a62\u6578\u64da<\/h4>\n<pre><code>local cursor = conn:execute(\"SELECT * FROM table_name\")\nrow = cursor:fetch({}, \"a\")\nwhile row do\n    print(row.column_name)\n    row = cursor:fetch(row, \"a\")\nend<\/code><\/pre>\n<h4 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-dlNroFxJTl\">\u63d2\u5165\u6578\u64da<\/h4>\n<pre><code>conn:execute(\"INSERT INTO table_name (column1, column2) VALUES (value1, value2)\")<\/code><\/pre>\n<h4 id=\"%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-dlNroFxJTl\">\u95dc\u9589\u9023\u63a5<\/h4>\n<pre><code>cursor:close()\nconn:close()\nenv:close()<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96-dlNroFxJTl\">\u6027\u80fd\u512a\u5316<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u6027\u80fd\u512a\u5316\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u512a\u5316\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\uff1a<\/strong>\u9810\u8655\u7406\u8a9e\u53e5\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u4e26\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li><strong>\u6279\u91cf\u64cd\u4f5c\uff1a<\/strong>\u5c0d\u65bc\u5927\u91cf\u6578\u64da\u7684\u63d2\u5165\u6216\u66f4\u65b0\uff0c\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u7d22\u5f15\uff1a<\/strong>\u70ba\u5e38\u7528\u7684\u67e5\u8a62\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u52a0\u5feb\u67e5\u8a62\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-dlNroFxJTl\">\u7d50\u8ad6<\/h2>\n<p>Lua\u4f5c\u70ba\u4e00\u7a2e\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u8b80\u5beb\u3002\u901a\u904e\u4f7f\u7528LuaSQL\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u4e26\u9032\u884c\u5404\u7a2e\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u96a8\u8457\u5c0d\u6027\u80fd\u8981\u6c42\u7684\u63d0\u9ad8\uff0c\u5408\u7406\u7684\u512a\u5316\u7b56\u7565\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u4e3b\u6a5f\u7684\u9078\u64c7\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u63a2\u7d22\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u89e3\u6c7a\u65b9\u6848\uff0c\u52a9\u60a8\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u7372\u5f97\u66f4\u4f73\u7684\u6027\u80fd\u9ad4\u9a57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Lua\u8f15\u9b06\u64cd\u63a7\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u5be6\u73fe\u9ad8\u6548\u8b80\u5beb\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\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-62353","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62353","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=62353"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62353\/revisions"}],"predecessor-version":[{"id":62354,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62353\/revisions\/62354"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=62353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=62353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=62353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}