voron00
28th October 2017, 20:59
I'm currenctly working on SQLite support for libcod. MySQL is nice but might be too heavy and/or hard for some people. SQLite is a lightweight and compact local database engine, and it's also quite easy to use.
The basic support has been added, there are no async support yet, but i'm working on it.
On Arch Linux, SQLite is preinstalled. On ubuntu proably apt-get install sqlite or something like that. It won't be compiled if you don't have the library though.
Unlike MySQL, SQLite is VERY simple, and doesn't need any wrappers, here's a little how to:
Open your database:
level.ranksdb = sqlite_open("databases/ranks.db");
Execute your query:
rows = sqlite_query(level.ranksdb, "SELECT id, rank FROM ranks");
Where the first argument is the database pointer and second is your query.
The returned result of rows is the sqlite fields and rows of each field.
So for our query, the result will be like: rows[0][0] where the first number is the FIELD number and second is ROW number.
If you do:
for (i = 0; i < rows.size; i++)
printf("% - %\n", rows[i][0], rows[i][1]);
The result will be:
1 - Idiot
2 - Noob
3 - Sergant Slaughter
4 - Lowak
5 - Loler
6 - Private
7 - OVER 9000
8 - JERKASS
9 - Crap
10 - Faggot
11 - Maggot
12 - Nanny's boyfriend
13 - Leader of gay-parade
14 - Captain Obvious
15 - Ass
16 - Cock of Duty 2
Close your database:
sqlite_close(level.ranksdb);
You can close your database on endmap for example, but that's not neccecary, a new sqlite_open will just return the previous database pointer on new map/restart.
Also made some nice error messages if you f*ck up your query for example:
******* script runtime error *******
gsc_sqlite_query() failed to fetch query data: near "FRM": syntax error: (file 'novoselscripts/zom.gsc', line 126)
rows = sqlite_query(db, "SELECT * FRM ranks");
*
called from:
(file 'maps/mp/gametypes/_callbacksetup.gsc', line 15)
[[level.callbackStartGameType]]();
*
started from:
(file 'maps/mp/gametypes/_callbacksetup.gsc', line 10)
CodeCallback_StartGameType()
*
************************************
Sys_Error: Error during initialization:
script runtime error
(see console for details)
gsc_sqlite_query() failed to fetch query data: near "FRM": syntax error
The basic support has been added, there are no async support yet, but i'm working on it.
On Arch Linux, SQLite is preinstalled. On ubuntu proably apt-get install sqlite or something like that. It won't be compiled if you don't have the library though.
Unlike MySQL, SQLite is VERY simple, and doesn't need any wrappers, here's a little how to:
Open your database:
level.ranksdb = sqlite_open("databases/ranks.db");
Execute your query:
rows = sqlite_query(level.ranksdb, "SELECT id, rank FROM ranks");
Where the first argument is the database pointer and second is your query.
The returned result of rows is the sqlite fields and rows of each field.
So for our query, the result will be like: rows[0][0] where the first number is the FIELD number and second is ROW number.
If you do:
for (i = 0; i < rows.size; i++)
printf("% - %\n", rows[i][0], rows[i][1]);
The result will be:
1 - Idiot
2 - Noob
3 - Sergant Slaughter
4 - Lowak
5 - Loler
6 - Private
7 - OVER 9000
8 - JERKASS
9 - Crap
10 - Faggot
11 - Maggot
12 - Nanny's boyfriend
13 - Leader of gay-parade
14 - Captain Obvious
15 - Ass
16 - Cock of Duty 2
Close your database:
sqlite_close(level.ranksdb);
You can close your database on endmap for example, but that's not neccecary, a new sqlite_open will just return the previous database pointer on new map/restart.
Also made some nice error messages if you f*ck up your query for example:
******* script runtime error *******
gsc_sqlite_query() failed to fetch query data: near "FRM": syntax error: (file 'novoselscripts/zom.gsc', line 126)
rows = sqlite_query(db, "SELECT * FRM ranks");
*
called from:
(file 'maps/mp/gametypes/_callbacksetup.gsc', line 15)
[[level.callbackStartGameType]]();
*
started from:
(file 'maps/mp/gametypes/_callbacksetup.gsc', line 10)
CodeCallback_StartGameType()
*
************************************
Sys_Error: Error during initialization:
script runtime error
(see console for details)
gsc_sqlite_query() failed to fetch query data: near "FRM": syntax error