[PD-dev] Connecting up an SQL Database to PD
Frank Barknecht
fbar at footils.org
Tue Nov 6 16:44:05 CET 2007
Hallo,
Frank Barknecht hat gesagt: // Frank Barknecht wrote:
> pdlua is easier to build than pyext so you may try Claude's pdlua with
> luasql: http://www.keplerproject.org/luasql/
So, I hacked together a quick example with not much error checking.
Tested on Debian with the package: liblua5.1-sql-sqlite2
(Claude, feel free to include it in pdlua with whatever license.)
Ciao
--
Frank Barknecht _ ______footils.org__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lsql-help.pd
Type: application/puredata
Size: 1541 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-dev/attachments/20071106/03108bb6/attachment.bin>
-------------- next part --------------
-- SQL example for pdlua
-- Written by Frank Barknecht in 2007, use however you like.
-- load driver
require "luasql.sqlite"
local M = pd.Class:new():register("lsql")
function M:initialize(name)
-- create environment object
self.env = assert (luasql.sqlite())
self.con = nil
self.outlets = 2
self.inlets = 1
return true
end
function M:in_1_open(atoms)
-- connect to data source
self.con = assert (self.env:connect(atoms[1]))
end
function M:in_1_sql(atoms)
if not self.con then
self:error("open a database file first")
return
end
local command = table.concat(atoms, " ")
-- use : instead of ,
command = command:gsub(":", ",")
local cur = assert (self.con:execute(command))
if type(cur) == "number" then
-- report affected rows to second outlet:
self:outlet(2, "float", {cur})
else
local row = cur:fetch({})
while row do
self:outlet(1, "list", row)
row = cur:fetch(row)
end
-- close cursor
cur:close()
end
end
More information about the Pd-dev
mailing list