A modification of aps_include.nss library script that allows to use sqlite database embedded into NWN Enhanced Edition without manually changing scripts syntax.
You can also use this as a wrapper allowing simpler way of writing SQL queries than what NWN-EE came with.
Note, that this is the default aps_include that comes from this project and most scripters probably heavily modified it, so if this is the case, obviously you cannot use it as-is.
So if this is your case, here is what you need to to merge my changes with your copy of aps_include:
1) add line
sqlquery last_sql_query;
somewhere under SQL_SUCCESS constant.
2) copy definition of the functions SQLExecDirect, SQLFetch, SQLGetData and possibly SQLInit as well
and that is it. Save and recompile all scripts in your module, this should make all your db calls work and use internal sqlite3 database from NWN Enhanced Edition.
However,
note that syntax might have changed, if you were using MYSQL some of your syntax might cease to work, I noticed three major differences:
- create table syntax is different completely, but creating tables from nwscript is not that common, eitherway if you do it google the new syntax
- RAND() needs to be RANDOM() in sqlite3
- CURRENT_TIMESTAMP() needs to be CURRENT_TIMESTAMP in sqlite3
- the LIMIT clausule is only possible to use with SELECT, see this link for workarounds if you use it with DELETE or UPDATE commands
And note that this does not copy your tables and data from mysql or another external database you installed on your server into sqlite3 itself. Neither does NWN EE allows this, EE only converts old foxpro database files from older patches.
So you need to convert the tables and data manually yourself. There are some tools over the internet that allows this, all I can say is that DataGrip is not the good tool for this, it messed up the column names and data types and I had to manually fix that. It also removed all primary keys. So try something else. Can't really advice much when everyone was using different type of database. I was using MySQL myself.
If you didn't use database in NWNX and you want to use this project as a wrapper to write SQL queries more elegantly, then this is the syntax:
SQLExecDirect("some query");
if the query isn't select, this is all you need to do, if the query is select you want to read data this way:
while(SQLFetch())
{
sItem = SQLGetData(1);
nStack = StringToInt(SQLGetData(2));}
Notice that the data column parameter offset is +1 over vanilla NWN-EE that starts with 0. Other than that it makes the coding simpler compared to writing the same code with NWN-EE functions.
Attachment | Size |
---|---|
aps_include.zip (176) | 3.11 KB |
If you use NWNXEE Object you can also support Setting/Getting persistent objects (With inventory and everything).
#include "nwnx_object"
// Return a string value when given a object
string APSObjectToString(object oObject);
// Return a object value when given the string form of the object
object APSStringToObject(string sObject);
// Get oObject's persistent vector variable sVarName
// Optional parameters:
// sTable: Name of the table where variable is stored (default: pwdata)
// * Return value on error: 0
vector GetPersistentVector(object oObject, string sVarName, string sTable = "pwdata");
// Get oObject's persistent object variable sVarName
// Optional parameters:
// sTable: Name of the table where variable is stored (default: pwdata)
// * Return value on error: 0
object GetPersistentObject(object oObject, string sVarName, string sTable = "pwdata");
string APSObjectToString(object oObject)
{
return NWNX_Object_Serialize(oObject);
}
object APSStringToObject(string sObject)
{
return NWNX_Object_Deserialize(sObject);
}
void SetPersistentObject(object oObject, string sVarName, object oObjectT, int iExpiration =
0, string sTable = "pwdata")
{
SetPersistentString(oObject, sVarName, APSObjectToString(oObjectT), iExpiration, sTable);
}
object GetPersistentObject(object oObject, string sVarName, string sTable = "pwdata")
{
return APSStringToObject(GetPersistentString(oObject, sVarName, sTable));
}
I [12:43:00] [NWNX_ServerLogRedirector] [ServerLogRedirector.cpp:38] (Server) statement aborts at 15: [SELECT sql FROM "main".sqlite_schema WHERE type='table'AND name<>'sqlite_sequence' AND coalesce(rootpage,1)>0] database schema has changed while executing (unknown)
Console error is popping up. Not sure how to track this down.
Well this looks like it is somehow related to NWNX not my scripting library? I don't know, did you ask at nwnx discord?
Yeah, it's not your include, it's old NWNx / LETO cruft in the PRC that needs to be removed and hasn't been.