Results 1 to 6 of 6

Thread: [WIP] SQLite support for libcod

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts

    [WIP] SQLite support for libcod

    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:
    PHP Code:
    level.ranksdb sqlite_open("databases/ranks.db"); 
    Execute your query:
    PHP Code:
    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:

    PHP Code:
    for (0rows.sizei++)
        
    printf("% - %\n"rows[i][0], rows[i][1]); 
    The result will be:

    Code:
    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:
    PHP Code:
    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:
    Code:
    ******* 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
    sudo apt-get rekt

  2. The Following 2 Users Say Thank You to voron00 For This Useful Post:

    kung foo man (28th October 2017),Lonsofore (29th October 2017)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •