{"id":194535,"date":"2024-11-13T07:28:22","date_gmt":"2024-11-12T23:28:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194535\/"},"modified":"2024-11-13T07:28:23","modified_gmt":"2024-11-12T23:28:23","slug":"%e7%94%a8lua%e8%85%b3%e6%9c%ac%e6%93%8d%e4%bd%9cmssql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%88lua%e5%af%abmssql%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194535\/","title":{"rendered":"\u7528Lua\u8173\u672c\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\uff08lua\u5bebmssql\uff09"},"content":{"rendered":"<h1 id=\"%e7%94%a8lua%e8%85%b3%e6%9c%ac%e6%93%8d%e4%bd%9cmssql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%88lua%e5%af%abmssql%ef%bc%89-WorAQcCSOJ\">\u7528Lua\u8173\u672c\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\uff08lua\u5bebmssql\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u958b\u767c\u74b0\u5883\u4e2d\uff0cLua\u4f5c\u70ba\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u6f54\u6027\u548c\u9ad8\u6548\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u5c24\u5176\u662f\u5728\u904a\u6232\u958b\u767c\u548c\u5d4c\u5165\u5f0f\u7cfb\u7d71\u4e2d\uff0cLua\u7684\u61c9\u7528\u975e\u5e38\u666e\u904d\u3002\u7136\u800c\uff0cLua\u4e5f\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u7279\u5225\u662f\u8207MSSQL\uff08Microsoft SQL Server\uff09\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Lua\u8173\u672c\u4f86\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"lua%e8%88%87mssql%e7%9a%84%e9%80%a3%e6%8e%a5-WorAQcCSOJ\">Lua\u8207MSSQL\u7684\u9023\u63a5<\/h2>\n<p>\u8981\u4f7f\u7528Lua\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u6709\u5408\u9069\u7684\u9a45\u52d5\u7a0b\u5e8f\u3002Lua\u672c\u8eab\u4e26\u4e0d\u76f4\u63a5\u652f\u6301MSSQL\uff0c\u4f46\u53ef\u4ee5\u901a\u904e\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5eab\u4f86\u5be6\u73fe\u3002\u6700\u5e38\u7528\u7684\u5eab\u662fLuaSQL\uff0c\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecMSSQL\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528LuaSQL\u9023\u63a5MSSQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>local luasql = require \"luasql.odbc\"\nlocal env = luasql.odbc()\nlocal conn = env:connect(\"DSN=your_dsn;UID=your_username;PWD=your_password;\")<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u60a8\u9700\u8981\u66ff\u63db`your_dsn`\u3001`your_username`\u548c`your_password`\u70ba\u5be6\u969b\u7684\u6578\u64da\u6e90\u540d\u7a31\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002\u9019\u6a23\u5c31\u53ef\u4ee5\u5efa\u7acb\u8207MSSQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/p>\n<h2 id=\"%e5%9f%b7%e8%a1%8csql%e6%9f%a5%e8%a9%a2-WorAQcCSOJ\">\u57f7\u884cSQL\u67e5\u8a62<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u6578\u64da\uff1a<\/p>\n<pre><code>local cursor = conn:execute(\"SELECT * FROM your_table\")\nlocal row = cursor:fetch({}, \"a\")\n\nwhile row do\n    print(row.column_name)  -- \u66ff\u63db\u70ba\u5be6\u969b\u7684\u5217\u540d\n    row = cursor:fetch(row, \"a\")\nend<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`your_table`\u662f\u60a8\u8981\u67e5\u8a62\u7684\u8868\u540d\uff0c\u800c`column_name`\u662f\u60a8\u8981\u7372\u53d6\u7684\u5217\u540d\u3002\u9019\u6bb5\u4ee3\u78bc\u5c07\u904d\u6b77\u67e5\u8a62\u7d50\u679c\u4e26\u6253\u5370\u51fa\u6bcf\u4e00\u884c\u7684\u6307\u5b9a\u5217\u3002<\/p>\n<h2 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-WorAQcCSOJ\">\u63d2\u5165\u6578\u64da<\/h2>\n<p>\u9664\u4e86\u67e5\u8a62\u6578\u64da\uff0c\u60a8\u9084\u53ef\u4ee5\u4f7f\u7528Lua\u8173\u672c\u5411MSSQL\u6578\u64da\u5eab\u63d2\u5165\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>local insert_query = \"INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2')\"\nconn:execute(insert_query)<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`your_table`\u662f\u76ee\u6a19\u8868\u540d\uff0c`column1`\u548c`column2`\u662f\u8981\u63d2\u5165\u7684\u5217\u540d\uff0c\u800c`value1`\u548c`value2`\u662f\u5c0d\u61c9\u7684\u503c\u3002\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u6578\u64da\u63d2\u5165\u5230\u6307\u5b9a\u7684\u8868\u4e2d\u3002<\/p>\n<h2 id=\"%e8%99%95%e7%90%86%e9%8c%af%e8%aa%a4-WorAQcCSOJ\">\u8655\u7406\u932f\u8aa4<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Lua\u7684`pcall`\u51fd\u6578\u4f86\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\uff1a<\/p>\n<pre><code>local success, err = pcall(function()\n    conn:execute(insert_query)\nend)\n\nif not success then\n    print(\"Error occurred: \" .. err)\nend<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5617\u8a66\u57f7\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u767c\u751f\u932f\u8aa4\uff0c\u5c07\u6355\u7372\u4e26\u6253\u5370\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WorAQcCSOJ\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Lua\u8173\u672c\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u9078\u64c7\u3002\u901a\u904eLuaSQL\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u5230MSSQL\uff0c\u57f7\u884c\u67e5\u8a62\u548c\u63d2\u5165\u64cd\u4f5c\uff0c\u4e26\u9032\u884c\u932f\u8aa4\u8655\u7406\u3002\u9019\u4f7f\u5f97Lua\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u65b9\u9762\u7684\u61c9\u7528\u8b8a\u5f97\u66f4\u52a0\u5ee3\u6cdb\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Lua\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Lua\u8173\u672c\u64cd\u4f5cMSSQL\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u6578\u64da\u7ba1\u7406\u8207\u67e5\u8a62\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-194535","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194535","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=194535"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194535\/revisions"}],"predecessor-version":[{"id":194536,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194535\/revisions\/194536"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}