SQL failing to create column?

Hey everyone :smiley:

So I want to save some player-data in the SQL database. When the gamemode initializes, I run this (SERVER):


hook.Add("Initialize", "create cog_player_data uppon server start", function()

	if not sql.TableExists("cog_player_data") then
	
		local result = sql.Query("CREATE TABLE cog_player_data (SteamID varchar, Money int)")
		
		if result == false then
			print("COG-sql: "..sql.LastError())
		else
			print("COG: Player database succesfully created!")
		end
		
	else
		print("COG: Player data table already exists.")
	end
	
end)

What works out fine (no errors). But when I then want to insert the data of the local player in to the newly created table, I get an error. Here’s the code (SERVER):


concommand.Add("cog_createdata", function(ply)
	local result = sql.Query("INSERT INTO cog_player_data (SteamID, Money) VALUES (SteamID = '"..ply:SteamID().."', Money = 1000)")
	
	if result == false then
		print("COG-sql: "..sql.LastError())
	else
		print("COG: Player data profile successfully created!")
	end
end)

And I get this error:


COG-sql: no such column: SteamID

Any ideas why it is not recognizing the SteamID column?

Any help is much appreciated :smiley:

that’s not how sql works, the values do not include the column names ("Steamid = " text)

Hm, that makes sense, but I followed the example on the wiki:

How would one do it properly?

The reason you are mentioning column names by doing ( steamid, money ) is so later you don’t have to do VALUES( steamid = shit, money = crap ) but VALUES( shit, crap ) instead

[editline]16th May 2016[/editline]

Also your sql.TableExists() and sql.Query( “CREATE TABLE” ) can be easily replaced with:
[lua]sql.Query( “CREATE TABLE IF NOT EXISTS t ( data )” )[/lua]

Awesome! Thanks Neth!