[R] MySQL-Function

08/12/2012 22:59 Mijago#1
Da die derzeitigen MySQL-Funktionen "etwas" veraltet sind, hier mal eine neue:

PHP Code:
function split(str, delim, maxNb)
    if 
str == nil then return str end
    
if string.find(str, delim) == nil then return { str } end
    
if maxNb == nil or maxNb < 1 then maxNb = 0 end
    local result 
= {}
    
local pat = "(.-)" .. delim .. "()"
    
local nb = 0
    local lastPos
    
for part, pos in string.gfind(str, pat) do
        
nb = nb + 1
        result
[nb] = part
        lastPos 
= pos
        
if nb == maxNb then break end
    end
    
if nb ~= maxNb then result[nb + 1] = string.sub(str, lastPos) end
    
return result
end



mysql_query 
= function(query)
    if 
not pre then
        local rt 
= io.open('CONFIG','r'):read('*all')
        
pre,_= string.gsub(rt,'.+PLAYER_SQL:%s(%S+)%s(%S+)%s(%S+)%s(%S+).+','-h%1 -u%2 -p%3 -D%4')
    
end
    math
.randomseed(os.time())
    
local fi,t,out = 'mysql_data_'..math.random(10^9)+math.random(2^4,2^10),{},{}
    
os.execute('mysql '..pre..' --e='..string.format('%q',query)..' > '..fi) -- für MySQL51
    
-- os.execute('mysql '..pre..' -e='..string.format('%q',query)..' > '..fi) -- für MySQL55
    
for av in io.open(fi,'r'):lines() do table.insert(t,split(av,'\t')) end; os.remove(fi);
    for 
i = 2, table.getn(t) do table.foreach(t[i],function(a,b)
        
out[i-1]               = out[i-1] or {}
        
out[i-1][a]            = tonumber(b) or b or 'NULL'
        
out[t[1][a]]           = out[t[1][a]] or {}
        
out[t[1][a]][i-1]      = tonumber(b) or b or 'NULL'
    
end) end
    
return out
end 

Ich habe die Funktion absichtlich kurz gehalten (verglichen mit meiner alten Version ist die hier ein Witz). Sie liest die Zugangsdaten aus der CONFIG-Datei des Cores aus.

Neuer Output:
Output[Zeile][Reihe]

###
Jetzt auch (fast) Abwärtskompatibel
Output.Spaltenname[zeile]
###


Danke an:
Paylasici für den Anstoß, die Userrechte aus der CONFIG-Datei zu lesen
αddι für das ständige generve, ich solle die MySQL-Func überarbeiten
.Active für die Unterhaltung und das Ablenken xD
Und natürlich an mein Prinzesschen ♥.Mercy.♥ für die viele gute Laune :p

lg Mijago

### Update
Nun Abwärtskompatibel zu meiner Questlib!

Bei MySQL55 müsst ihr ab und an --e= durch -e ersetzen!
08/12/2012 22:59 .Active#2
Love you
mijaja <3
08/12/2012 22:59 Pаylasici#3
Super gemacht Schatz. :D
08/12/2012 23:00 αddι#4
Uuuuh danke, extra für mich? xD

€: Ok habs verstanden :(
08/12/2012 23:02 xXKimariXx#5
Danke Mijago^^
08/12/2012 23:07 .αpeх'#6
Geil *o*

Wie immer geniales Zeig von dir ;)
08/12/2012 23:09 vpser#7
Thank You Mijago
08/12/2012 23:32 x3DQ#8
Was bringt das blicks net D:
Aber weil alle THX geben bekommst du auch eins von mir :D
08/12/2012 23:47 Mijago#9
Quote:
Originally Posted by x3DQ View Post
Was bringt das blicks net D:
Aber weil alle THX geben bekommst du auch eins von mir :D
Dass du in Quests auf die MySQL-Datenbank des Servers zugreifen kannst :)
Eigendlich nur für fortgeschrittene Questwriter nützlich.
08/13/2012 00:04 x3DQ#10
achso :D
08/15/2012 23:14 poowaa#11
Thanks Mijago it's cool.

I can use it with UPDATE and INSERT INTO.

Can u help me?

Code:
quest teszteljunk begin
	state start begin
		when 53001.use begin
		asd = mysql_query("SELECT name FROM player.player WHERE id="..pc.get_player_id()..";")
			say(""..asd.."")
			say(""..asd[1].."")
			say(""..asd[1][1].."")
		end
	end
end
What's wrong? I can't write down the output :/
08/16/2012 00:31 Mijago#12
asd[1][1]
or
asd.name[1]

or simply pc.get_name()....
12/09/2012 20:02 Mijago#13
Fix für: # mysql: ambiguous option

Habe mal die Funktion aus der Questlib hier eingefügt.
[Only registered and activated users can see links. Click Here To Register...]

Und unten den Fix angefügt, weshalb viele Denken, diese Lib geht nicht (leute, der Fix ist seit Monaten bekannt xD)

Er ist auch in der Funktion mittlerweile.

PHP Code:
mysql_query = function(query)
    if 
not pre then
        local rt 
= io.open('CONFIG','r'):read('*all')
        
pre,_= string.gsub(rt,'.+PLAYER_SQL:%s(%S+)%s(%S+)%s(%S+)%s(%S+).+','-h%1 -u%2 -p%3 -D%4')
    
end
    math
.randomseed(os.time())
    
local fi,t,out = 'mysql_data_'..math.random(10^9)+math.random(2^4,2^10),{},{}
    
os.execute('mysql '..pre..' --e='..string.format('%q',query)..' > '..fi) -- für MySQL51
    
-- os.execute('mysql '..pre..' -e'..string.format('%q',query)..' > '..fi) -- für MySQL55
    
for av in io.open(fi,'r'):lines() do table.insert(t,split(av,'\t')) end; os.remove(fi);
    for 
i = 2, table.getn(t) do table.foreach(t[i],function(a,b)
        
out[i-1]               = out[i-1] or {}
        
out[i-1][a]            = tonumber(b) or b or 'NULL'
        
out[t[1][a]]           = out[t[1][a]] or {}
        
out[t[1][a]][i-1]      = tonumber(b) or b or 'NULL'
    
end) end
    
return out
end 
MySQL51: --e=""
MySQL55: -e""
12/10/2012 15:29 agronaque512#14
Where we put this code pls?
12/10/2012 18:41 ThinSmoke#15
Quote:
Originally Posted by Mijago View Post
Habe mal die Funktion aus der Questlib hier eingefügt.
[Only registered and activated users can see links. Click Here To Register...]

Und unten den Fix angefügt, weshalb viele Denken, diese Lib geht nicht (leute, der Fix ist seit Monaten bekannt xD)

Er ist auch in der Funktion mittlerweile.

PHP Code:
mysql_query = function(query)
    if 
not pre then
        local rt 
= io.open('CONFIG','r'):read('*all')
        
pre,_= string.gsub(rt,'.+PLAYER_SQL:%s(%S+)%s(%S+)%s(%S+)%s(%S+).+','-h%1 -u%2 -p%3 -D%4')
    
end
    math
.randomseed(os.time())
    
local fi,t,out = 'mysql_data_'..math.random(10^9)+math.random(2^4,2^10),{},{}
    
os.execute('mysql '..pre..' --e='..string.format('%q',query)..' > '..fi) -- für MySQL51
    
-- os.execute('mysql '..pre..' -e'..string.format('%q',query)..' > '..fi) -- für MySQL55
    
for av in io.open(fi,'r'):lines() do table.insert(t,split(av,'\t')) end; os.remove(fi);
    for 
i = 2, table.getn(t) do table.foreach(t[i],function(a,b)
        
out[i-1]               = out[i-1] or {}
        
out[i-1][a]            = tonumber(b) or b or 'NULL'
        
out[t[1][a]]           = out[t[1][a]] or {}
        
out[t[1][a]][i-1]      = tonumber(b) or b or 'NULL'
    
end) end
    
return out
end 
MySQL51: --e=""
MySQL55: -e""

Wer wirklich mit dieser Funktion arbeiten will sollte wenigstens mySQL Grundkentnisse haben und dem wäre das auch alleine klar geworden.


"Copy & Paste "nutzung ist doch langweilig.


Nunja, gute Arbeit und sehr sinnige Nutzung :-)