Register for your free account! | Forgot your password?

You last visited: Today at 18:02

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Help]Lua SQL

Discussion on [Help]Lua SQL within the Metin2 Private Server forum part of the Metin2 category.

Reply
 
Old   #1
 
.Risan.'s Avatar
 
elite*gold: 30
Join Date: Jul 2010
Posts: 1,627
Received Thanks: 1,450
[Help]Lua SQL

Also Folgendes,
Hier wurde erklärt wie man os.execute benutzen kann...



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?
.Risan. is offline  
Old 10/12/2011, 16:23   #2
 
elite*gold: 0
Join Date: Oct 2011
Posts: 55
Received Thanks: 16
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)
Manuel92x is offline  
Old 10/12/2011, 17:03   #3
 
iYoshix3's Avatar
 
elite*gold: 0
Join Date: Jun 2010
Posts: 1,264
Received Thanks: 2,019
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
iYoshix3 is offline  
Old 10/12/2011, 17:37   #4
 
elite*gold: 32
Join Date: Dec 2010
Posts: 1,294
Received Thanks: 862
os.execute gibt kein Wert zurück
√π. is offline  
Old 10/12/2011, 17:41   #5
 
elite*gold: 0
Join Date: Oct 2011
Posts: 55
Received Thanks: 16
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).
Manuel92x is offline  
Old 10/12/2011, 17:44   #6
 
elite*gold: 32
Join Date: Dec 2010
Posts: 1,294
Received Thanks: 862
Egal wie, es wird nicht gehen
√π. is offline  
Old 10/12/2011, 18:07   #7
 
elite*gold: 0
Join Date: Oct 2011
Posts: 55
Received Thanks: 16
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.
Manuel92x is offline  
Old 10/13/2011, 02:38   #8
 
atag's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 104
Received Thanks: 126
If you want to pass the return variables, u have to use ("command") instead of ()
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...
atag is offline  
Thanks
1 User
Old 10/13/2011, 04:02   #9
 
elite*gold: 0
Join Date: Oct 2011
Posts: 55
Received Thanks: 16
Quote:
Originally Posted by atag View Post
If you want to pass the return variables, u have to use ("command") instead of ()
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.
Manuel92x is offline  
Old 10/13/2011, 04:04   #10
 
ThinSmoke's Avatar
 
elite*gold: 49
Join Date: Sep 2008
Posts: 906
Received Thanks: 600
Quote:
Originally Posted by atag View Post
If you want to pass the return variables, u have to use ("command") instead of ()
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
ThinSmoke is offline  
Old 10/13/2011, 05:42   #11
 
elite*gold: 0
Join Date: Oct 2011
Posts: 55
Received Thanks: 16
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.
Manuel92x is offline  
Old 10/13/2011, 05:59   #12
 
ThinSmoke's Avatar
 
elite*gold: 49
Join Date: Sep 2008
Posts: 906
Received Thanks: 600
Ja, ist es allerdings.

Doch ist es eine nette Funktion für einen InGame Marktplatz.
ThinSmoke is offline  
Old 10/14/2011, 00:10   #13
 
atag's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 104
Received Thanks: 126
Sorry, i was wrong... popen is not supported by the core
atag is offline  
Old 10/14/2011, 00:35   #14
 
ThinSmoke's Avatar
 
elite*gold: 49
Join Date: Sep 2008
Posts: 906
Received Thanks: 600
Quote:
Originally Posted by atag View Post
Sorry, i was wrong... popen is not supported by the core
yap, but with php shell ...
ThinSmoke is offline  
Old 10/14/2011, 00:48   #15
 
atag's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 104
Received Thanks: 126
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:


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
atag is offline  
Thanks
6 Users
Reply




All times are GMT +1. The time now is 18:03.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.