通过LUA脚本输出SQL表

时间:2018-11-19 14:52:14

标签: mysql printing lua

我正在寻找有关LUA脚本的帮助,该脚本应以可读格式输出表的结果。

此脚本仅将结果打印为表格参考:

database.open("provider=nis;database=nis;driver=none")

query = database.query("SELECT distinct cmcs.name, ms.schedule_name, mw.start_time, mw.end_time from MAINTENANCE_WINDOW mw INNER JOIN CM_DEVICE cmd on mw.dev_id = cmd.dev_id INNER JOIN CM_COMPUTER_SYSTEM cmcs on cmcs.cs_id = cmd.cs_id INNER JOIN MAINTENANCE_SCHEDULE ms on mw.schedule_id = ms.schedule_id where mw.end_time > GETDATE();")

print (query)

以上输出:

table:0x7fe8f40e6f00

我有以下脚本,但没有输出我期望看到的内容:

database.open("provider=nis;database=nis;driver=none")

query = database.query("SELECT distinct cmcs.name, ms.schedule_name, mw.start_time, mw.end_time from MAINTENANCE_WINDOW mw INNER JOIN CM_DEVICE cmd on mw.dev_id = cmd.dev_id INNER JOIN CM_COMPUTER_SYSTEM cmcs on cmcs.cs_id = cmd.cs_id INNER JOIN MAINTENANCE_SCHEDULE ms on mw.schedule_id = ms.schedule_id where mw.end_time > GETDATE();")

if query == nil then
   print ("Query did not complete, or had no results")
else
   for idx,row in pairs (query) do
      printf("Hostname: %s \t IP: %s",row['name'],row['schedule_name'])
   end 
end

下面是在SQL Server本身上运行时SQL输出的屏幕快照(如果有的话):

SQL Output

1 个答案:

答案 0 :(得分:0)

知道了

database.open("provider=nis;database=nis;driver=none")

function tdump(t)
       local function dmp(t, l, k)
     if type(t) == "table" then
       print(string.format("%s%s:", string.rep(" ", l*2), tostring(k)))
       for k, v in pairs(t) do
         dmp(v, l+1, k)
       end
     else
       print(string.format("%s%s:%s", string.rep(" ", l*2), tostring(k), tostring(t)))
     end
   end
 dmp(t, 1, "Maintenance Monitor")
         end 



--To OUTFILE
query = database.query("SELECT distinct cmcs.name, ms.schedule_name, mw.start_time, mw.end_time from MAINTENANCE_WINDOW mw INNER JOIN CM_DEVICE cmd on mw.dev_id = cmd.dev_id INNER JOIN CM_COMPUTER_SYSTEM cmcs on cmcs.cs_id = cmd.cs_id INNER JOIN MAINTENANCE_SCHEDULE ms on mw.schedule_id = ms.schedule_id where mw.end_time > GETDATE() INTO OUTFILE '/tmp/Maintenance.txt';")

tdump(query)

希望对其他人有帮助!