• SQL Error: no such column
    4 replies, posted
Hey guys, I'm currently working on a script that saves spawned vehicles from a player when he leaves the server. For that I use the UPDATE Function from sqlite. My Problem is when I want to update the table it always says: "no such column: prop_vehicle_jeep". So here is my code (At this time it saves all vehicles on the map when a player leaves): [lua] function SaveVehicle( ply ) print( "Saving vehicle!" ) local VehNumber = 0 local steamID = ply:SteamID() for k,v in pairs( ents.GetAll() ) do if v:IsVehicle() then VehNumber = VehNumber + 1 print( "Saving veh"..VehNumber ) local veh_pos = v:GetPos() local veh_model = v:GetModel() local veh_type = v:GetClass() print( "Vehicle Type: "..veh_type ) result = sql.Query("UPDATE perma_vehicles SET veh"..VehNumber.." = "..veh_type..", veh"..VehNumber.."_pos_x = "..veh_pos.x..", veh"..VehNumber.."_pos_y = "..veh_pos.y..", veh"..VehNumber.."_pos_z = "..veh_pos.z..", veh"..VehNumber.."_model = "..veh_model.." WHERE unique_id = '"..steamID.."'") if !result then Msg( sql.LastError( result ).."\n" ) end end end end hook.Add( "PlayerDisconnected", "SaveVehicle", SaveVehicle ) [/lua] And this is my table create code: [lua] sql.Query( "CREATE TABLE perma_vehicles ( unique_id varchar(255), donator int, veh1 varchar(255), veh2 varchar(255), veh1_pos_x int, veh1_pos_y int, veh1_pos_z int, veh2_pos_x int, veh2_pos_y int, veh2_pos_z int, veh1_model varchar(255), veh2_model varchar(255) )" ) [/lua] I hope you guys can help me. Thanks in advance!
Print a sql.LastError just after you create a table, obviously something fucks up there.
Okay, I'll try that... Edit: No error... I use that code: [lua] function tables_exist() if ( sql.TableExists( "perma_vehicles" ) ) then Msg( "\nPVeh - Table(s) exist(s)!\n" ) else if ( !sql.TableExists( "perma_vehicles" ) ) then query = "CREATE TABLE perma_vehicles ( unique_id varchar(255), donator int, veh1 varchar(255), veh2 varchar(255), veh1_pos_x int, veh1_pos_y int, veh1_pos_z int, veh2_pos_x int, veh2_pos_y int, veh2_pos_z int, veh1_model varchar(255), veh2_model varchar(255) )" result = sql.Query( query ) if ( sql.TableExists( "perma_vehicles" ) ) then Msg( "Succes! Table 1 created \n" ) else Msg( "Somthing went wrong with the perma_vehicles query! \n" ) Msg( sql.LastError( result ) .. "\n" ) end end end end [/lua]
Based on the code you've posted, it looks like where it's going wrong is here: [lua] result = sql.Query("UPDATE perma_vehicles SET veh"..VehNumber.." = "..veh_type..", veh"..VehNumber.."_pos_x = "..veh_pos.x..", veh"..VehNumber.."_pos_y = "..veh_pos.y..", veh"..VehNumber.."_pos_z = "..veh_pos.z..", veh"..VehNumber.."_model = "..veh_model.." WHERE unique_id = '"..steamID.."'") [/lua] I've made up the query by substituting those vars, which makes this: [code] UPDATE perma_vehicles [i]SET veh1 = prop_vehicle_jeep, [/i]veh1_pos_x = 50, blah blah [/code] The error is "SET veh1 = prop_vehicle_jeep". I think what's happening is that it's trying to set veh1 to the column prop_vehicle_jeep, which doesn't exist in the table. You should have single quotes around your values, like you have for unique_id. The hopefully fixed version: [lua] result = sql.Query("UPDATE perma_vehicles SET veh"..VehNumber.." = '"..veh_type.."', veh"..VehNumber.."_pos_x = '"..veh_pos.x.."', veh"..VehNumber.."_pos_y = '"..veh_pos.y.."', veh"..VehNumber.."_pos_z = '"..veh_pos.z.."', veh"..VehNumber.."_model = '"..veh_model.."' WHERE unique_id = '"..steamID.."'") [/lua] If you want to make your code more readable (and easier to maintain), you can use [b][url=http://wiki.garrysmod.com/?title=String.format]String.format [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b].
Thanks dude! The problem is solved :)
Sorry, you need to Log In to post a reply to this thread.