LuaでPostgreSQLに接続(libpqベース)
LuaでDB接続といえば、LuaSQL(ODBC)が有名だと思うんですが、PostgreSQLに接続するんだったら
やっぱりlibpqベースだよねぇーってことで、いろいろ試しましたが、一番良かった奴のメモ。
luaのPostgreSQLのドライバは以下にまとまっています。
http://lua-users.org/lists/lua-l/2011-01/msg01031.html
http://lua-users.org/wiki/DatabaseAccess
で、気に入ったのが、 lua-poastgres。
https://github.com/norman/lua-postgres
まだまだ、機能不足ですが、今後に期待&自分でいじれそうってのが大きいですが。。。
導入手順
githubの中にrockspecあるんだけど、luarocksで導入できなかったので、普通にソースコンパイル。
前提で、/usr/local/pgsql/${VERSIONS}/ にDBをインストールするのが自分のお約束なので、
それに従っています。(今回は9.1.3使ってます)
git clone https://github.com/norman/lua-postgres.git export PG_CONFIG=/usr/local/pgsql/9.1.3/bin/pg_config export PATH=$PATH:/usr/local/pgsql/9.1.3/bin make paco -D make install
で、あとは、test配下にいろいろあるので、そっち参考にしてもらえばいいんですが、
一応サンプルが動くかをのテストコード。
local postgres = require "postgres" local conn = postgres.connection('dbname=postgres host=localhost user=postgres') --local result, error = conn:execute("SELECT * FROM pg_tables") local result, error = conn:execute("SELECT now() ") if err then error(err) end for i = 1, #result do local row = result:fetch() print(row[1]) -- prints the name end
C言語頑張らないとなぁー。。。