• SQL Saving Problem
    4 replies, posted
I am clearly doing something wrong but what? (I did look around for quite some time) Saving - What I think it's wrong [lua] for i=1,40 do local amount = ply.amount[ply.inv[i]] if !amount then amount = 0 end local id = ply.inv[i] if !id then id = "nil" end sql.Query( "INSERT INTO inventory ( `uniqueid`, `n`, `amount`, `id` ) VALUES ( '".. uniqueID .."', '"..i.."', '"..amount.."', '"..id.."' )" ) print(sql.Query( "SELECT id FROM inventory WHERE uniqueid = '".. uniqueID .."', n = '"..i.."'" )) -- returns false print(sql.Query( "SELECT amount FROM inventory WHERE uniqueid = '".. uniqueID .."', n = '"..i.."'" )) -- returns false end [/lua] Rest of it - Possibly right [lua] function sqlinventory( ply ) local uniqueID = ply:UniqueID() for i = 1,40 do ply.inv[i] = sql.QueryValue( "SELECT id FROM inventory WHERE uniqueid = '".. uniqueID .."', n = '"..i.."'" ) ply.amount[i] = sql.QueryValue( "SELECT amount FROM inventory WHERE uniqueid = '".. uniqueID .."', n = '"..i.."'" ) if ply.inv[i] == "false" or ply.inv[i] == "nil" or !ply.inv[i] then print(i.." nop") ply.inv[i] = nil ply.amount[i] = nil else print(i.." ok") print(ply.inv[i]) print(ply.amount[i]) end end end function checktables() for k, v in pairs (ents.FindByClass("func_breakable")) do v:Remove() end if !sql.TableExists("inventory") then sql.Query( "CREATE TABLE inventory ( uniqueid string, n int, amount int, id string )" ) if sql.TableExists("inventory") then Msg("Table `inventory` successfuly created!\n") else Msg("Could create table `inventory`!\n") end end end hook.Add("InitPostEntity", "checktables", checktables ) function newsqlinv( SteamID, ply ) local uniqueID = ply:UniqueID() for i=1,40 do local amount = ply.amount[ply.inv[i]] if !amount then amount = 0 end local id = ply.inv[i] if !id then id = "nil" end sql.Query( "INSERT INTO inventory ( `uniqueID`, `n`, `amount`, `id` ) WHERE uniqueID = '".. uniqueID .."' VALUES ( '".. uniqueID .."', '"..i.."', '"..amount.."', '"..id.."' )" ) end end function sqlstart( ply ) local uniqueID = ply:UniqueID() Result = sql.Query( "SELECT uniqueID, n, amount, id FROM inventory WHERE uniqueID = '".. uniqueID .."'" ) if (Result) then print("works") sqlinventory( ply ) else print("need new") newsqlinv( uniqueID, ply ) end end hook.Add("PlayerSpawn","spawnthatfag", function(ply) timer.Simple( 0.2, function() sqlstart( ply ) end) end) [/lua]
Looks tough, wish I could help. :'(
In your SQL select multiple things by using AND not commas. [lua]sql.Query( "SELECT `id` FROM `inventory` WHERE `uniqueid` = '".. uniqueID .."' AND `n` = '"..i.."'" )[/lua]
I don't see why you're doing more then 120 queries every 0.2 seconds, try to pack in as much data into one table otherwise you'll just have crazy amount of lag due the insane amount of networking. I suggest you rethink and restructure your database with the idea in mind that less queries means less stress on the networking. Also consider only sending queries when something is changed rather then every 0.2 seconds as well because that's another major stress on your server.
Print sql.LastError()
Sorry, you need to Log In to post a reply to this thread.