Difference between revisions of "LuaSQL"
Jump to navigation
Jump to search
(Created page with "Use this for connecting to databases in lua https://keplerproject.github.io/luasql/index.html") |
|||
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 | ||
+ | - I used to use pgmoon and some others but the above is much better | ||
− | + | # 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=unode port=44441 user=someadmin password=jtest123")) | ||
+ | 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() | ||
+ | ``` |
Revision as of 03:31, 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
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=unode port=44441 user=someadmin password=jtest123")) 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()