SQLite Creating tables with lua

Hi all, I’ve made this code.

[lua]
// database.lua
// Is planned to come on a webserver but for now we’ll use the gmod sql.

if (DB == nil) then
DB = {};
DB.Player = {};
DB.Player.Table = “player_data”
end

function DB.Initialize()
local query = "CREATE TABLE " … DB.Player.Table … "
" …
[[
(
SteamID text UNIQUE NOT NULL,
Name text,
Wealth bigint,
Health tinyint,
Hunger tinyint,
Stamina tinyint,
Experience int,
Level int
)
]]

if (sql.TableExists( DB.PlayerTable ) == false) then
	sql.Begin();
	sql.Query(query)
	sql.Commit();
	
	print(query);
	print(sql.TableExists( DB.PlayerTable ) );
	print("Table " .. DB.Player.Table .. " was created.");
end

end
hook.Add(“Initialize”, “DBInitalizeHook”, DB.Initialize)
[/lua]

What I planned it to do was if the table “player_data” does not exist, then it has to create the table in SQLite, meaning it’s stored inside the sv.db.
But every time my code gets called then it prints.



// OUTPUT:
CREATE TABLE player_data
		(
		SteamID text UNIQUE NOT NULL,
		Name text,
		Wealth bigint,
		Health tinyint,
		Hunger tinyint,
		Stamina tinyint,
		Experience int,
		Level int
		)
		
false
Table player_data was created.


Meaning that the table is not being created.
Is this a SQLite error from my side?

Also, does anyone have a link where you can learn SQLite and not SQL.
Currently I’m using the W3Schools.

Run **[sql.LastError

http://wiki.garrysmod.com/favicon.ico](http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexd821.html)** after your query and see what it returns.

Well thank you.
It casts some light over the error.

But apparently the error is that sql.TableExists doesn’t work right.



output
table player_data already exists
CREATE TABLE player_data
		(
		SteamID text UNIQUE NOT NULL,
		Name text,
		Wealth bigint,
		Health tinyint,
		Hunger tinyint,
		Stamina tinyint,
		Experience int,
		Level int,
		Test text
		)
		
false
Table player_data was created.


[lua]
function DB.Initialize()
local query = "CREATE TABLE " … DB.Player.Table … "
" …
[[
(
SteamID text UNIQUE NOT NULL,
Name text,
Wealth bigint,
Health tinyint,
Hunger tinyint,
Stamina tinyint,
Experience int,
Level int,
Test text
)
]]

if (sql.TableExists( DB.PlayerTable ) == false) then
	sql.Begin();
	sql.Query(query)
	sql.Commit();
	
	print(sql.LastError())
	print(query);
	print(sql.TableExists( DB.PlayerTable ) );
	print("Table " .. DB.Player.Table .. " was created.")
end

end
hook.Add(“Initialize”, “DBInitalizeHook”, DB.Initialize)
[/lua]

Also how come sql.LastError() is not on the SQL Library GMOD 13 Wiki
When it works perfectly fine.

I don’t know a whole lot about SQLite, but that output doesn’t make ANY sense to me. Maybe try dropping the table before you run it again? Or try running a query to obtain information from the table and see if you get anything, be it data or an error.

Also, apparently I missed it, but sql.LastError() is on the GMod 13 Wiki.

Oh, nvm I just found out that I failed to spot a “.”.

[lua]
if (sql.TableExists( DB.PlayerTable ) == false) then
[/lua]

should have been

[lua]
if (sql.TableExists( DB.Player.Table ) == false) then
[/lua]

because of

[lua]
DB = {};
DB.Player = {};
DB.Player.Table = “player_data”
[/lua]

Can’t you use “CREATE TABLE IF NOT EXISTS”?

He already fixed it, it was checking to see if the table existed in the end that was to fault.