Currently trying to learn some SQL by using [URL="http://www.facepunch.com/threads/777892-Sql-Tutorial"]this[/URL] tutorial, but ran into an error involving sql and I haven't been able to figure out the issue.
I'm creating a logger that inserts player information into a sql data base on connect and disconnect. I first initialized the sql table by using this function...
[CODE]hook.Add( "Initialize", "SAVEIPINT", function ( p )
if (sql.TableExists("ip_sql")) then
Msg("ip_sql table already exist !")
else
if (!sql.TableExists("ip_sql")) then
query = "CREATE TABLE ip_sql (date string, day string, time string, action string, name string, steamid varchar(255), ip string )"
sql.Query(query)
if (sql.TableExists("ip_sql")) then
Msg("Success ! ip_sql created! \n")
else
Msg("\n ERROR #1 :Something went wrong with creating ip_sql! \n")
Msg("Line (6):ip_sql.lua ... \n")
Msg( sql.LastError( result ) .. "\n" )
end
end
end
end)[/CODE]
When the player joins/disconnects it calls the function insert_info and passes some information with it....
[CODE]function insert_info ( Ply, Date , Day, Time, Action )
query = "INSERT INTO ip_sql ( date, day, time, action, name, steamid ,ip ) VALUES ( date = '" .. Date .. "',day = '" .. Day .. "',time = '" .. Time .. "',action = '" .. Action .. "',name = '" .. Ply:Nick() .. "',steamid = '" .. Ply:SteamID() .. "',ip = '" .. Ply:IPAddress() .. "')"
result = sql.Query(query)
if(result)then
Msg("result1\n\n\n")
end
if (result) then
Msg("ip_sql table info inserted!")
else
Msg("\nERROR #2: Something went wrong when inserting into ip_sql! \n")
Msg("Line (57):ip_sql.lua ... \n")
Msg( sql.LastError(result) .. "\n" )
end
end[/CODE]
But when I run a test to see if I can store any information I get the following error...
[IMG]http://i.gyazo.com/518f3da5e730f012798997cc85195779.png[/IMG]
But didn't I already make a column for that? Did I do something wrong in the initialization of the table? I've tried a couple of things, but I've been running into dead ends. Can someone give me an idea what I did wrong?
Well, it seems to me that you created table previously then added date and thought it would re-create it with the new column.
I suggest you do the command below and then restart server
[code]lua_run sql.Query( "DROP TABLE ip_sql" )[/code]
[QUOTE=Netheous;46545696]Well, it seems to me that you created table previously then added date and thought it would re-create it with the new column.
I suggest you do the command below and then restart server
[code]lua_run sql.Query( "DROP TABLE ip_sql" )[/code][/QUOTE]
Just ran that line in console, and it dropped the table.
When I restarted and tested again it had the same error. Is it possibly a syntax error with my INSERT query?
EDIT:
[B]Problem solved! [/B]
In my old code I was searching for my result from an INSERT method.
[CODE]query = "INSERT INTO ip_sql ( date, day, time, action, name, steamid ,ip ) VALUES ( date = '" .. Date .. "',day = '" .. Day .. "',time = '" .. Time .. "',action = '" .. Action .. "',name = '" .. Ply:Nick() .. "',steamid = '" .. Ply:SteamID() .. "',ip = '" .. Ply:IPAddress() .. "')"
result = sql.Query(query)
if(result)then
Msg("result1\n\n\n")
end
if (result) then
Msg("ip_sql table info inserted!")
else
Msg("\nERROR #2: Something went wrong when inserting into ip_sql! \n")
Msg("Line (57):ip_sql.lua ... \n")
Msg( sql.LastError(result) .. "\n" )
end
[/CODE]
In which result would be turned into a boolean. Well since INSERT would neither prove true nor false my code would automatically would just to my ERROR#2 Msg. To fix that I resigned my code to search for the date submitted (not exactly what I wanted but for testing purposes it worked great!).
[CODE] query = "INSERT INTO ip_sql (`date`, `day`, `time`, `action`, `name`, `steamid` ,`ip`) VALUES ('"..Date.."','"..Day .."','"..Time.. "','" .. Action .. "','" .. Ply:Nick() .. "','" .. Ply:SteamID() .. "','" .. Ply:IPAddress() .. "')"
sql.Query(query)
result = sql.Query( "SELECT date FROM ip_sql WHERE date = '"..Date.."'" )
if(result)then
Msg("result1\n\n\n")
end
if (result) then
Msg("ip_sql table info inserted!")
else
Msg("\nERROR #2: Something went wrong when inserting into ip_sql! \n")
Msg("Line (57):ip_sql.lua ... \n")
Msg( sql.LastError(result) .. "\n" )
end[/CODE]
Now I get the msg "ip_sql table info inserted!"
Thanks for the help!
Sorry, you need to Log In to post a reply to this thread.