Difference between revisions of "LuaSQL"
Jump to navigation
Jump to search
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Use this for connecting to databases in lua | Use this for connecting to databases in lua | ||
- https://keplerproject.github.io/luasql/index.html | - https://keplerproject.github.io/luasql/index.html | ||
| − | - I used to use pgmoon and some others but the above is much better | + | - I used to use pgmoon and some others but the above is much better and supports scram-sha-256 |
# Install | # Install | ||
| Line 15: | Line 15: | ||
local driver = require "luasql.postgres" | local driver = require "luasql.postgres" | ||
env = assert (driver.postgres()) | env = assert (driver.postgres()) | ||
| − | con = assert (env:connect("dbname= | + | con = assert (env:connect("dbname=mydbname host=127.0.0.1 port=5432 user=someadmin password=somepass")) |
res = con:execute"DROP TABLE people" | res = con:execute"DROP TABLE people" | ||
res = assert (con:execute[[ | res = assert (con:execute[[ | ||
Latest revision as of 03:37, 18 January 2021
Use this for connecting to databases in lua - https://keplerproject.github.io/luasql/index.html - I used to use pgmoon and some others but the above is much better and supports scram-sha-256
Install
apt install postgresql-server-dev-13 # I think we need ths sudo luarocks PGSQL_INCDIR=/usr/include/postgresql/ install luasql-postgres
Postgres
#!/usr/bin/env lua5.1
local driver = require "luasql.postgres"
env = assert (driver.postgres())
con = assert (env:connect("dbname=mydbname host=127.0.0.1 port=5432 user=someadmin password=somepass"))
res = con:execute"DROP TABLE people"
res = assert (con:execute[[
CREATE TABLE people(
name varchar(50),
email varchar(50)
)
]])
list = {
{ name="Jose das Couves", email="jose@couves.com", },
{ name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },
{ name="Maria das Dores", email="maria@dores.com", },
}
for i, p in pairs (list) do
res = assert (con:execute(string.format([[
INSERT INTO people
VALUES ('%s', '%s')]], p.name, p.email)
))
end
cur = assert (con:execute"SELECT name, email from people")
row = cur:fetch ({}, "a")
while row do
print(string.format("Name: %s, E-mail: %s", row.name, row.email))
row = cur:fetch (row, "a")
end
cur:close() -- already closed because all the result set was consumed
con:close()
env:close()