SQL SELECT Question

Hello,

Been looking at this for a while now and still unsure why it’s happening:




function ENT:Touch( entity )
 	if ( entity:IsValid()) then
        entityModel = (entity:GetModel())
        print("The entity that is currently touching the create is: " .. entityModel)
        sql.Query( "INSERT INTO player_quest_data (`unique_id`, `quest_name`, `data`) VALUES ('STEAM_0:1:22686651', 'scrap_metal_collection', '" .. entityModel .. "')" )
        result2 = sql.Query( "SELECT unique_id FROM player_quest_data WHERE quest_name = 'scrap_metal_collection'" )
        print(result2)
        result = sql.Query( "SELECT data FROM player_quest_data WHERE unique_id = 'STEAM_0:1:22686651' AND quest_name = 'scrap_metal_collection'" )
        print(result)
	end
end 



So printing entityModel prints “The entity that is currently touching the create is: models/props_c17/streetsign005c.mdl” however printing the results after I gave selected them from the database returns “table: 0xe61166d0” and “table: 0xe4593358”

I am missing something? Why are these returning these table strings rather then the steamID and what entityModel was set

Thanks for the help.

Note: I know I’ve hard-coded in the steamID and quest_name it’s just for testing.

[editline]5th May 2014[/editline]

Hold on, brain fart I think I’ve got it.

[editline]5th May 2014[/editline]

Okay, scrap that. I still doesn’t work. I know that whenever I restart the server the table is dropped nad re-made to ensure it’s clean. I have also added SELECT min(unique_id) and min(data) to ensure I only get one value returned. Any ideas?

[editline]5th May 2014[/editline]

No worries: Fixed it



function ENT:Touch( entity )
 	if ( entity:IsValid()) then
        //entityModel = (tostring(entity:GetModel()))
        entityModel = entity:GetModel()
        print("The entity that is currently touching the create is: " .. entityModel)
        sql.Query( "INSERT INTO player_quest_data (`unique_id`, `quest_name`, `data`) VALUES ('STEAM_0:1:22686651', 'scrap_metal_collection', '" .. entityModel .. "')" )
        result2 = sql.QueryValue( "SELECT unique_id FROM player_quest_data WHERE quest_name = 'scrap_metal_collection'" )
        print(result2)
        result = sql.QueryValue( "SELECT data FROM player_quest_data WHERE unique_id = 'STEAM_0:1:22686651' AND quest_name = 'scrap_metal_collection'" )
        print(result)
        if ( entity:IsPlayer() == false ) then
            entity:Remove()
        end
	end
end 


Yes, a query will typically return a table. You didn’t use LIMIT 1 at the end, so it’ll need to return a table of all entries. I need to look deeper into the SQLite functionality in terms of the build-in-functions; but sql.QueryValue should return a straight value, like this:

From lua/includes/extensions/player.lua
[lua]–[[---------------------------------------------------------
GetPData
- Saves persist data for this player
-----------------------------------------------------------]]
function meta:GetPData( name, default )

name = Format( "%s[%s]", self:UniqueID(), name )
local val = sql.QueryValue( "SELECT value FROM playerpdata WHERE infoid = " .. SQLStr(name) .. " LIMIT 1" )
if ( val == nil ) then return default end

return val

end[/lua]

The only real difference is the LIMIT 1; so that may be why you’re getting a table to deal with instead of a value. Make sure you escape any data when inserted, or selected, or updated if it comes from the user, or if the user has ANY influence over it at all.