Bueno, aca les dejo algunos ejemplos de uso del eDB (normal, no el OOP). Utilicé una base de datos en mi localhost llamada mails, con algunos campos como id, txt, mail, title y no recuerdo cual otro. Estos ejemplos los probé usando SciTE y esa es la razón por la cual verán funciones como "print", "table.foreach" y "os.exit". La función ptbl es para imprimir tablas y creo la publiqué hace mucho por aquí.
--[[Example initialization:
local a,e = eDB.connect = function("db", "root", "pwd", "localhost", 3306)
if not a then
print("Error", e);
os.exit();
end]]--
--[[--Example1
local a, e = eDB.execute("SELECT * FROM mails");
if a then
for row in eDB.fetch(a, {}, "n") do
print(row[1], row[2]);
end
a:close();
else
print(e);
end]]--
--[[--Example2
local props = {};
props.Fields = {"id", "txt"};--Example "id,title" or "*" or {"id", "title"}
props.Where = "";--Example1: "title='abc'" or {title='abc'} or {title="!= 'abc'"}
props.Limit = "";--Example 5 or 5,2
props.From = "mails";--Example tablename
props.GroupBy = "";--Example "author" or "author,date" or {"author", "date"}
props.Order = "";--Example "date" or "date ASC, author DESC" or {"author", "date DESC"}
props.ret = "a";--Example "a" or "n";
local t, e = eDB.find("all", props)
print(ptbl(t), e);
t, e = eDB.find("first", props)
print(ptbl(t), e);
t, e = eDB.find("count", props)
print(ptbl(t), e);
t, e = eDB.find("list", props);
print(ptbl(t), e);]]--
--[[Example call to retrieve all the databases
local t = eDB.listDatabases();
table.foreach(t, print);]]--
--[[Example call to setup a database:
eDB.setDatabase("test_eDB");--This could be any database
]]--
--[[Example call eDB.execute() and eDB.fetch():
--Imagine a database named "test_eDB" with 1 table named "users" (only 2 fields: id, name);
local cursor, error = eDB.execute("SELECT * FROM "..eDB.Database..".users)");
if cursor then
for row in eDB.fecth(cursor, {}) do
print(row['id'].."-"..row['name']);
end
cursor:close();
else
--Error
end
]]--
--[[--Example call smart function eDB.insert()
--Example inserting a simple record:
local t = {name="abc"};
local affected_rows, error = eDB.insert("users", t, true);
if affected_rows then
print("Total inserted rows: "..affected_rows);--Will insert 1 records
end
--Example inserting multiple records at once
local t = {};
t[1] = {name = "abc"};
t[2] = {name = "def"};
t[3] = {name= "ghi"};
local affected_rows, error = eDB.insert("users", t, true);
if affected_rows then
print("Total inserted rows: "..affected_rows);--Will insert 3 records
end
--Now an example with an error while inserting the records and rollingback the inserts
local t = {};
t[1] = {name = "abc"};
t[2] = {names = "def"};--Here is the error
t[3] = {name= "ghi"};
local affected_rows, error = eDB.insert("users", t, true);
if affected_rows then
print("Total inserted rows: "..affected_rows);--Won't insert a record
else
print("Zero inserted rows: "..error);
end
--Now an example with an error while inserting the records WITHOUT rollingback the inserts
local t = {};
t[1] = {name = "abc"};
t[2] = {names = "def"};--Here is the error
t[3] = {name= "ghi"};
local affected_rows, error = eDB.insert("users", t, false);
if affected_rows then
print("Total inserted rows: "..affected_rows);--Will insert 1 record
if error then
print("The following errors were produced while inserting: "..error);--The insert breaks if an error is found
end
end
]]--
--Example Finish DB connection
--[[
eDB.finish();
]]--