Skip to content

Reading and writing to a flat sqlite3 table

Been away a bit and now see that EE uses sqlite3 natively. I looked for some in-depth notes on how this is done but can't seem to find any. I did read that it converts get/set campaign var to sqlite3 but can't see how this breaks down into table name etc, or if it supports SQL queries. Can anyone point me in the right direction?

Comments

  • CalgacusCalgacus Member Posts: 273
    edited January 2021
    Start here: nwnlexicon.
    First, create a database by running that command in a module, yes it's a little weird that you have to run (just F9 to test the module) the module first but that seems to be the way to do it. After that you can edit the database file in an external tool like SQLiteStudio to create and populate your own tables.
    Then you can issue sql commands against it in nwscript.
    Here is some ugly sample code. Image an sqlite3 file with a recipes table in it with multiple string columns such as Name, Description and an integer column numberOfItems.
    string query = "SELECT * from recipes WHERE numberOfItems == @numberOfItems ";
    
    /////  The following command will create the "classical.sqlite" database file if it does not already exist under the "/Neverwinter Nights/database" folder //////
    recipequery = SqlPrepareQueryCampaign("classical", query);  
    string err = SqlGetError(recipequery);
    SpeakString("err 1  "+ err, TALKVOLUME_SHOUT);
    
    int number_Of_Items = 2; 
    SqlBindInt(recipequery, "@numberOfItems", number_Of_Items );
    err = SqlGetError(recipequery);
    SpeakString("err 2  "+ err, TALKVOLUME_SHOUT);
     
    
    while(SqlStep(recipequery)){
            SpeakString("Found recipe: "+ SqlGetString(recipequery, 0) +" " + SqlGetString(recipequery, 1), TALKVOLUME_SHOUT);
            //if harden recipe
            if(SqlGetString(recipequery, 0) == "harden"){
                SpeakString("Found a Harden recipe:", TALKVOLUME_SHOUT);
                // NOW DO SOMETHING INTERESTING with the harden recipe - harden a sword for instance  
            }
    
  • vonstormvonstorm Member Posts: 66
    Thank you
Sign In or Register to comment.