How to connect & query MySQL from within Lua?

前端 未结 3 1948
南笙
南笙 2021-02-07 18:48

How can I connect to a MySQL database from using Lua programming language?

If a good/popular library exists, what is it?

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 19:11

    Minimal woking example for LuaSQL - simple interface from Lua to a DBMS.

    package.cpath = package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so"
    
    luasql = require "luasql.mysql"
    
    env = assert (luasql.mysql())
    con = assert (env:connect("dbname","user","password"))
    cur = assert (con:execute("SHOW TABLES"))
    
    row = cur:fetch ({}, "a")
    while row do
      print(string.format("Name: %s", row.Tables_in_dbname))
      row = cur:fetch (row, "a")
    end
    

    Line 1 used if module luasql.mysql not found. Also environment variable LUA_CPATH may be used.

提交回复
热议问题