Abend,
ich habe gerade noch einer meiner ersten Klassen gefunden.
Es handelt sich um eine MySQL-Klasse. Das einzige was ihr anpassen müsst, ist der constructor, bzw. die Daten im Constructor.
Viel Spaß damit, vllt. braucht es ja wer.
Lua
sMySQL = {};
sMySQL.__index = sMySQL;
function sMySQL:constructor()
local connectionData = {};
connectionData.connectionType = ""; -- mostly "mysql"
connectionData.ip = "";
connectionData.dbname = "";
connectionData.user = "";
connectionData.pass = "";
connectionData.handler = dbConnect(connectionData.connectionType, "host="..connectionData.ip..";dbname="..connectionData.dbname, connectionData.user, connectionData.pass );
setmetatable(connectionData, self);
return connectionData;
end
function sMySQL:isConnected()
return (self.handler and true or false);
end
function sMySQL:Destructor()
if self:isConnected() then
self.handler = nil;
return true;
else
outputDebugString("The 'Destructor' method can't disconnect, cause he isnt connected to any database.", 1, 255, 0, 0);
end
end
function sMySQL:Query(qry, args)
if self:isConnected() == false then
local args = table.concat({args}, " ");
if string.find(qry, "SELECT") then
local sql = dbQuery(self.handler, qry, args);
if sql then
return true;
else
return false;
end
else
outputDebugString("The 'Query' method is only for 'SELECT'. Use the 'Exec' method for 'UPDATE' or 'INSERT'.", 1, 255, 0, 0);
end
else
outputDebugString("The class 'sMySQL' isn't connected to a database!", 1, 255, 0, 0);
end
end
function sMySQL:Exec(qry, args)
if self:isConnected() then
local args = table.concat({args}, " ");
if ( not string.find(qry, "SELECT") ) then
local exec = dbExec(self.handler, qry, args);
if exec then
return true;
else
return false;
end
else
outputDebugString("The 'Exec' method is only for 'UPDATE' or 'INSERT'. Use the 'Query' method for 'SELECT'.", 1, 255, 0, 0);
end
else
outputDebugString("The class 'sMySQL' isn't connected to a database!", 1, 255, 0, 0);
end
end
function sMySQL:Poll(sql, timeout)
if self:isConnected() then
if sql ~= nil then
if not timeout then timeout = -1; end
local result, num_rows, last_insert_id = dbPoll(sql, timeout);
if ( result and num_rows and last_insert_id ) then
return ( result and num_rows and last_insert_id );
end
else
outputDebugString("The 'Poll' expect an queryHandle which is not given.", 1, 255, 0, 0);
end
else
outputDebugString("The class 'sMySQL' isn't connected to a database!", 1, 255, 0, 0);
end
end
class = sMySQL:constructor();
Alles anzeigen
Die if-Abfragen der Methoden mögen eventuell ein wenig komisch sein aber nunja..;)
- UnLimiTeD