среда, 31 октября 2018 г.

lua & postgres

luarocks install luasql-postgres
luarocks install luasql-postgres PGSQL_INCDIR=/usr/include/postgresql/


#!/usr/bin/lua5.1
-- load driver
--require"luasql.postgres"
luasql = require "luasql.postgres"
-- create environment object
env = assert (luasql.postgres())
-- connect to data source
con = assert (env:connect('opensips','postgres','pass','localhost',5432))
-- retrieve a cursor
cur = assert (con:execute"SELECT * from  subscriber")
-- print all rows
row = cur:fetch ({}, "a")       -- the rows will be indexed by field names
while row do
  print(string.format("Username: %s, Msisdn: %s  \n", row.username, row.msisdn))
  row = cur:fetch (row, "a")    -- reusing the table of results
end
-- close everything
cur:close()
con:close()
env:close()