• Why would a SELECT statement wipe the contents of an SQLite row/column?
    4 replies, posted
I posted my code originally on the [URL="http://forums.ulyssesmod.net/index.php/topic,7778.0.html"]ULX forums[/URL], but I want to ask about a separate issue concerning the deletion of content from the local SQLite database. Basically, whenever the first condition after the null check is met, it either removes this player's utime row, or sets it to 0 (I can't tell which). [CODE]hook.Add( "PlayerInitialSpawn", "MAWTTTRegularInitialSpawn", function(p) local uid = p:UniqueID() local row = sql.QueryRow( "SELECT totaltime FROM utime WHERE player = " .. uid .. ";" ) if not row then return end local totaltime = row.totaltime if (p:IsUserGroup("user") and not p:IsUserGroup("regular")) and tonumber(totaltime) > 172000 then -- p:SetUserGroup("regular") ucl.addUser(uid, {}, {}, "regular") PrintMessage(HUD_PRINTTALK, "Congratulations " .. p:GetName() .. "! You have been promoted to a regular for playing a day on the server.") elseif p:IsUserGroup("regular") and tonumber(totaltime) < 172000 then -- p:SetUserGroup("user") ucl.removeUser(uid) PrintMessage(HUD_PRINTTALK, p:GetName() .. " has lost regular status for not playing on the server a day.") end end)[/CODE] Why would it be doing this? Does row.totaltime bind itself to the column?
That in itself shouldn't cause any issues. I would recommend adding tonumber to the totaltime = ... instead of needing to call it multiply times below, but a SELECT wouldn't unless someone was injecting data; I'm not sure if players can spoof uniqueid since it is util.CRC of gm_<steamid>_gm, but I'd still SQLStr( ) the data.... Most likely possibility is that adduser is adding blank tables and thereby wiping the information. Show us that function.
It's a method out of ULib: [CODE]function ucl.addUser( id, allows, denies, group ) ULib.checkArg( 1, "ULib.ucl.addUser", "string", id ) ULib.checkArg( 2, "ULib.ucl.addUser", {"nil","table"}, allows ) ULib.checkArg( 3, "ULib.ucl.addUser", {"nil","table"}, denies ) ULib.checkArg( 4, "ULib.ucl.addUser", {"nil","string"}, group ) id = id:upper() -- In case of steamid, needs to be upper case allows = allows or {} denies = denies or {} if allows == ULib.DEFAULT_GRANT_ACCESS.allow then allows = table.Copy( allows ) end -- Otherwise we'd be changing all guest access if denies == ULib.DEFAULT_GRANT_ACCESS.deny then denies = table.Copy( denies ) end -- Otherwise we'd be changing all guest access if group and not ucl.groups[ group ] then return error( "Group does not exist for adding user to (" .. group .. ")", 2 ) end -- Lower case'ify for k, v in ipairs( allows ) do allows[ k ] = v:lower() end for k, v in ipairs( denies ) do denies[ k ] = v:lower() end local name if ucl.users[ id ] and ucl.users[ id ].name then name = ucl.users[ id ].name end -- Preserve name ucl.users[ id ] = { allow=allows, deny=denies, group=group, name=name } ucl.saveUsers() local ply = ULib.getPlyByID( id ) if ply then ucl.probe( ply ) else -- Otherwise this gets called twice hook.Call( ULib.HOOK_UCLCHANGED ) end end[/CODE]
My guess is because it is a "new" user, it resets time. Look at where the -- Preserve name is written. If that isn't done for time, I'm sure it'll be overwritten. What are the contents of ucl.SaveUsers?
Here you go: [CODE]function ucl.saveUsers() for _, userInfo in pairs( ucl.users ) do table.sort( userInfo.allow ) table.sort( userInfo.deny ) end ULib.fileWrite( ULib.UCL_USERS, ULib.makeKeyValues( ucl.users ) ) end[/CODE] And makeKeyValues: [CODE]function ULib.makeKeyValues( t, tab, completed ) ULib.checkArg( 1, "ULib.makeKeyValues", "table", t ) tab = tab or "" completed = completed or {} if completed[ t ] then return "" end -- We've already done this table. completed[ t ] = true local str = "" for k, v in pairs( t ) do str = str .. tab if type( k ) ~= "number" then str = string.format( "%s%q\t", str, tostring( k ) ) end if type( v ) == "table" then str = string.format( "%s\n%s{\n%s%s}\n", str, tab, ULib.makeKeyValues( v, tab .. "\t", completed ), tab ) elseif type( v ) == "string" then str = string.format( "%s%q\n", str, v ) else str = str .. tostring( v ) .. "\n" end end return str end[/CODE] It has to be something about how Garry's Mod handles the database...
Sorry, you need to Log In to post a reply to this thread.