• SQL Names
    9 replies, posted
  • Avatar of antid2
  • Okay, everything works good, except it's not updating my database and i don't understand why, i use almost the same code for money, except i can't set my money, nor do i intend to lol...But here's the code names.lua [lua] function Regname(ply) local name = ply:GetNWString("name") if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";")) then sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");") else sql.Begin() ply:SetNWString("name",tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";"))) sql.Commit() end end hook.Add("PlayerInitialSpawn", "Regname", Regname) [/lua] commands.lua [lua] //commands function SetName(ply,cmd,args) local PlayerName = tostring(args[1]) ply:SetNWString("name", PlayerName) sql.Begin() sql.Query("UPDATE player_name SET name = "..PlayerName.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") sql.Commit() end concommand.Add("ChangeName", SetName) [/lua] and client side in cl_startmenu.lua [lua] Apply.DoClick = function( btn ) if FirstName:GetValue() == "" then ply:ChatPrint("You did not enter a first name") else RunConsoleCommand("ChangeName", FirstName:GetValue()) Frame:Remove() end end [/lua] It sets the name, etc..but in commands.lua it's not updating the database..
  • Avatar of antid2
  • oh sorry..i must have cut some SQL off...heres the full names.lua [lua] sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' INTEGER NOT NULL,PRIMARY KEY('steam'));") function UpdateName(ply) local name = ply:GetNWString("name") sql.Begin() sql.Query("UPDATE player_name SET name = "..name.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") sql.Commit() end hook.Add("PlayerDisconnected", "UpdateName", UpdateName) function Regname(ply) local name = ply:GetNWString("name") if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";")) then sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");") else sql.Begin() ply:SetNWString("name",tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";"))) sql.Commit() end end hook.Add("PlayerInitialSpawn", "Regname", Regname) [/lua]
  • Avatar of quincy18
  • Why are you using tonumber when your working with names ? never used the external database module so excuse me if thats why your using it.
  • Avatar of Averice
  • Should name be an INTEGER NOT NULL? Just guessing as you said you use the exact same for your money system, maybe you didn't change it when you copy and pasted it. And seeing as "steam" is the primary key I'm going to guess name should be TEXT NOT NULL; [lua] sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' TEXT NOT NULL,PRIMARY KEY('steam'));") [/lua]
  • Avatar of antid2
  • good catch averice, didn't notice that. [b]Edit:[/b] New file: [lua] sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' TEXT NOT NULL,PRIMARY KEY('steam'));") function UpdateName(ply) local name = ply:GetNWString("name") sql.Begin() sql.Query("UPDATE player_name SET name = "..name.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") sql.Commit() end hook.Add("PlayerDisconnected", "UpdateName", UpdateName) function Regname(ply) local name = ply:GetNWString("name") if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";")) then sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");") else sql.Begin() ply:SetNWString("name",tostring(sql.QueryValue("SELECT name FROM player_name WHERE steam = "..sql.SQLStr(ply:SteamID())..";"))) sql.Commit() end end hook.Add("PlayerInitialSpawn", "Regname", Regname) [/lua] Still no go boss.
  • Avatar of cheiftiger
  • You don't have to use sql.Begin() and sql.Commit() if you're just doing 1-2 queries. That's used for alot of queries at once (maybe 5 or more). And stop using tonumber() on strings. That will just return nil, which is why it's not saving probably. I don't know if you've seen it, but there's a tutorial on GMod SQL here: [url]http://wiki.garrysmod.com/?title=LUA:SQLite_Tutorial[/url] I recently made an entity using SQL, and I used varchar(255) for strings. I don't know if that makes a difference, but mine works. I also didn't do that PRIMARYKEY thing that you did. That's the best help I can give without coding the thing for you, so I wish you good luck.
  • Avatar of antid2
  • that would be amazing if the code worked :P, i modified it just to test, didn't work...so i figured maybe i messed up...i copied the code straight from the wiki and it says something went wrong with creating a players info, etc. and savestat doesn't work :(