[Help]Lua SQL

09/14/2011 12:44 .Risan.#1
Also Folgendes,
Hier wurde erklärt wie man os.execute benutzen kann...
[Only registered and activated users can see links. Click Here To Register...]


Nun ich wollte damit etwas ausprobieren und bleibe hier fest:

PHP Code:
return os.execute("mysql -u mt2putty --execute='SELECT * FROM player.magicepoint WHERE id = ".. pc.get_player_id() ..";'"
Bekomm ingame aber keine fehlermeldung.
In der Datenbank is meine playerid eingetragen sowie ein Wert..
Was hab ich da genau Falsch?
10/12/2011 16:23 Manuel92x#2
Ich bin mir nicht sicher, ob man überhaupt etwas SELECTEN kann im Spiel, schließlich muss das ganze ja ausgegeben werden.
Eventuell über

local cmd = ox.execute("statement")
say_reward("..cmd..")

Außerdem fehlt bei dir neben -u mt2putty das --password Statement (außer du verwendest kein Passwort)
10/12/2011 17:03 iYoshix3#3
Du hast ganz einfach kein local gesetzt. Außerdem hast du SELECT genommen und auch
*, das heißt, alles wird geladen und kann nicht wiedergegeben werden weil kein
specieller wert angegeben ist.

Mfg
10/12/2011 17:37 √π.#4
os.execute gibt kein Wert zurück
10/12/2011 17:41 Manuel92x#5
Quote:
Originally Posted by Yoshix3 View Post
Du hast ganz einfach kein local gesetzt. Außerdem hast du SELECT genommen und auch
*, das heißt, alles wird geladen und kann nicht wiedergegeben werden weil kein
specieller wert angegeben ist.

Mfg
Das mit dem local sollte stimmen.
SELECT * FROM xyz bedeutet, dass alles geladen wird.
Solltest du nur SELECT x,y,z FROM xyz nehmen, können die Werte ebenfalls, wie bei SELECT * geladen werden, nur nicht ausgegeben werden, sofern kein local vorhanden ist (gehe ich mal von aus, habe noch keine Ahnung von der Lua Materie).
10/12/2011 17:44 √π.#6
Egal wie, es wird nicht gehen
10/12/2011 18:07 Manuel92x#7
Quote:
Originally Posted by √π. View Post
Egal wie, es wird nicht gehen
Man kann es in eine Datei schreiben lassen, und sie anschließend auslesen lassen, das sollte funktionieren.
10/13/2011 02:38 atag#8
If you want to pass the return variables, u have to use [Only registered and activated users can see links. Click Here To Register...]("command") instead of [Only registered and activated users can see links. Click Here To Register...]()
example:
Code:
local f,e=io.popen("mysql -u mt2putty -e 'SELECT * FROM player.magicepoint WHERE id = ".. pc.get_player_id() ..";'")
if f then 
    res=f:read("*a")
    f:close()
end
say_reward(res or "SQL ERROR")
Of corse, u can use f:lines() in a loop if u get more lines:
Code:
for res in f:lines() do
    say_reward(res)
    wait()
end
f:close()
os.execute returns 0 if no errors, or an error status code, nothing more...
10/13/2011 04:02 Manuel92x#9
Quote:
Originally Posted by atag View Post
If you want to pass the return variables, u have to use [Only registered and activated users can see links. Click Here To Register...]("command") instead of [Only registered and activated users can see links. Click Here To Register...]()
example:
Code:
local f,e=io.popen("mysql -u mt2putty -e 'SELECT * FROM player.magicepoint WHERE id = ".. pc.get_player_id() ..";'")
if f then 
    res=f:read("*a")
    f:close()
end
say_reward(res or "SQL ERROR")
Of corse, u can use f:lines() in a loop if u get more lines:
Code:
for res in f:lines() do
    say_reward(res)
    wait()
end
f:close()
os.execute returns 0 if no errors, or an error status code, nothing more...
Can you explain me how i can change some things like socialid, password?
In game it won´t work, because i can't escape the string.
10/13/2011 04:04 ThinSmoke#10
Quote:
Originally Posted by atag View Post
If you want to pass the return variables, u have to use [Only registered and activated users can see links. Click Here To Register...]("command") instead of [Only registered and activated users can see links. Click Here To Register...]()
example:
Code:
local f,e=io.popen("mysql -u mt2putty -e 'SELECT * FROM player.magicepoint WHERE id = ".. pc.get_player_id() ..";'")
if f then 
    res=f:read("*a")
    f:close()
end
say_reward(res or "SQL ERROR")
Of corse, u can use f:lines() in a loop if u get more lines:
Code:
for res in f:lines() do
    say_reward(res)
    wait()
end
f:close()
os.execute returns 0 if no errors, or an error status code, nothing more...

Cant work, or? o.0
10/13/2011 05:42 Manuel92x#11
Quote:
Originally Posted by xPit View Post
Cant work, or? o.0
Ich werde das heute Abend mal ausprobieren, ich interessiere mich dafür total und es scheint mir ziemlich sinnvoll.
Wobei Ich aber sagen muss, dass es ziemlich Overkill mäßig ist, was das selecten im Spiel angeht.
10/13/2011 05:59 ThinSmoke#12
Ja, ist es allerdings.

Doch ist es eine nette Funktion für einen InGame Marktplatz. :)
10/14/2011 00:10 atag#13
Sorry, i was wrong... popen is not supported by the core :(
10/14/2011 00:35 ThinSmoke#14
Quote:
Originally Posted by atag View Post
Sorry, i was wrong... popen is not supported by the core :(
yap, but with php shell ... :)
10/14/2011 00:48 atag#15
Here is a tested one. Only for testing :)

os.tmpname() also not supported...

PHP Code:
quest sqltest begin
    state start begin
        
function mysql_query(text)
            
local tmp=number(11111111,99999999)
            
os.execute('mysql -u USERHERE -pPASSWORDHERE -N -e '..text..' 2>&1 > /tmp/'..tmp)
            
local f,e=io.open("/tmp/"..tmp)
            if 
f then
                
return function()
                    
local l=f:read("*l")
                    if 
l then
                        
return l
                    
else 
                        
f:close()
                        
os.execute("rm /tmp/"..tmp)
                    
end
                end
            
else 
                return 
nile
            end
        end
                        
        when 20016.chat
."mysql" begin
            local sql
='SELECT id, account_id FROM player.player WHERE name='..string.format("%q",pc.get_name())..' LIMIT 1;'
            
say("Hello "..pc.get_name().." ")
            
say("Your PID and AID is:")
            for 
f,e in sqltest.mysql_query(string.format("%q",sql)) do
                
say(or e)
            
end
            say
("query was: ")
            
say(sql)
            
say("")
        
end
    end
end 
result:
[Only registered and activated users can see links. Click Here To Register...]

ATAG

This one gives mutch better results, in an indexed table :)

Code:
--by ATAG
quest sqltest begin
	state start begin
		function mysql_query(text)
            local tmp=number(11111111,99999999)
            os.execute('mysql -u root -pPASSWORD -N -e '..string.format("%q",text)..' 2>&1 > /tmp/'..tmp)
			local res,i={},1
			local f,e=io.open("/tmp/"..tmp)
            if f then
				local line=f:read("*l")
				while line do
					res[i]={}
					string.gsub(line,"([^\t]+)\t*", function(s)
						table.insert(res[i],s)
					end)
					i=i+1
					line=f:read("*l")
				end
				f:close()
                os.execute("rm /tmp/"..tmp)
			end
			--[[if table.getn(res)==1 and table.getn(res[1])==1 then --only 1 variable returns
				res=res[1][1]
			end]]
			return res
        end 
		when 20016.chat."test list" begin
			local tbl=sqltest.mysql_query("SELECT id, account_id, name from player.player LIMIT 10;")
			say("userlist: "..table.getn(tbl))
			for i,v in ipairs(tbl) do
				say(v[1].." "..v[2].." "..v[3])
			end
			say("")
		end
	end
end