通过lua脚本输出sql表

5jdjgkvh  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(569)

我正在寻找有关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输出的屏幕截图(如果有任何输出是这样的话):

6vl6ewon

6vl6ewon1#

知道了:

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)

希望它能帮助别人!

相关问题