https://github.com/daurnimator/lua-pg
Nice API to PostgreSQL
https://github.com/daurnimator/lua-pg
Last synced: about 1 month ago
JSON representation
Nice API to PostgreSQL
- Host: GitHub
- URL: https://github.com/daurnimator/lua-pg
- Owner: daurnimator
- License: mit
- Created: 2017-08-12T11:18:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-16T13:18:36.000Z (about 8 years ago)
- Last Synced: 2025-02-22T06:42:15.116Z (10 months ago)
- Language: Lua
- Size: 8.79 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
A wrapper around [luapgsql](https://github.com/arcapos/luapgsql) to give a nicer API.
Compatible with [cqueues-pgsql](https://github.com/daurnimator/cqueues-pgsql)
## Usage
```lua
local pg = require "pg"
-- Make a connection to a database using default parameters
local conn = assert(pg.connectdb())
-- Execute a command
assert(conn:exec[[create temporary table example(col1 int, col2 text)]])
-- Prepare a command
local insert = assert(conn:prepare([[insert into example(col1, col2) values ($1, $2)]], pg.getdefaultoid(1), pg.oids.text))
assert(insert:exec(1, "one"))
assert(insert:exec(2, "two"))
-- Make a simple query
local res = assert(conn:exec[[select * from example]])
-- Print column names
print("", res:fields())
-- Iterate over result rows
for row_number, col1, col2 in res:tuples() do
print(row_number, col1, col2)
end
print()
-- Prepare a query
local myquery = assert(conn:prepare([[select $1 * 2]], pg.getdefaultoid(1)))
-- Execute the query and get result
for row_number, x in assert(myquery:exec(42)):tuples() do
print(row_number, x) -- 84
end
```
## Conventions
Operations return `nil, err, error_number` on failure (suitable to be used with [assert](http://www.lua.org/manual/5.3/manual.html#pdf-assert))