logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Looping through a small database

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Scripts and Modules
View previous topic :: View next topic  
Author Message
Smolin



Joined: 10 Jul 2005
Posts: 4

PostPosted: Tue Jun 27, 2006 18:26    Post subject: Looping through a small database Reply with quote

I'm pretty new to building in NWNX: I've been maintaining a few systems on our PW, but this is the first project that I've really begun from scratch. I've gotten a lot farther than I thought I'd be able to, but have gotten stymied on a small problem with apostrophes.

My database tracks about 17 deities & their number of worshippers. I need to loop through the data, entry by entry, and retrieve the number of worshippers for each deity. (Deities on rows, worshippers on columns)

I got it working... mostly. It returns the names and values for the first 8 or 9 deities in my database, but it gets hung up when it hits a name with an apostrophe.

I'm sure there's a way to use SQLEncodeSpecialCharacters() to fix it, but I'm not smart enough. Sad

Here's what I have so far:

Code:

#include "aps_include"

// Returns deity name from row number?
string DeityLoop(string sPlace, string sTable, int nCount)
    {
    string sCount = IntToString(nCount);
    string sSQL = "SELECT deity FROM " + sTable + " LIMIT " + sCount + ",1";
    SQLExecDirect(sSQL);
        if(SQLFetch())
            return SQLGetData(1);
        else
            return "";
    }

// Returns number of worshippers for given deity?
string DeityPop(string sPlace, string sTable, string sDeity)
    {
    string sData;
    string sSQL = "SELECT " + sPlace + " FROM " + sTable + " WHERE deity='" + sDeity + "'";
    SQLExecDirect(sSQL);
    if( SQLFetch() == SQL_SUCCESS)
            {
            sData = SQLGetData(1);
            return sData;
            }
    else
            return "";
    }

void main()
{
object oCleric = GetPCSpeaker();
object oPlaceable = OBJECT_SELF;
object oModule = GetModule();

string sTable = "religion";
string sPlace = "tursus";

int nId;
string sDeity;

string sData;
int nNum = 0;

string sNum;
string sPop = "0";


while ((sPop != "") && (nNum < 40) )
        {
        sDeity = DeityLoop(sPlace, sTable, nId);
        sPop = DeityPop(sPlace, sTable, sDeity);
        // SetLocalInt(oModule, "POP_"+sDeity, nPop);
        SendMessageToPC(oCleric, sDeity + " has " + sPop + " followers in " + sPlace + ".");
        nId = nId + 1;
        nNum = nNum + 1;  // failsafe counter, just in case I did some kind of infinite loop
        }


}



The DeityLoop() function works fine on its own, returning a full list of deities, including the Almighty Apostrophe-ities. Wink But the trouble comes when those names are entered into the DeityPop() function.

Any ideas would be greatly appreciated!
Back to top
View user's profile Send private message
Acrodania



Joined: 02 Jan 2005
Posts: 208

PostPosted: Tue Jun 27, 2006 18:59    Post subject: Reply with quote

Apostrophes are BAD in SQL databases. Easiest way is to use another character, like a reverse apostrophe, instead.
Back to top
View user's profile Send private message
Smolin



Joined: 10 Jul 2005
Posts: 4

PostPosted: Tue Jun 27, 2006 19:46    Post subject: Reply with quote

Thanks Acrodania. I think I'll just do that.

I tried encoding/decoding the names, but that didn't let me get data from the table, e.g. name in table was S'ierhan, but the input value would have been S~ierhan, so no match was found.

While it's a bit of a hassle to retroactively change few deity names in my mod & server vault, but it seems like that's the best option. I'll set something up in the OnClientEnter event to change the deity field for the players who chose them.

Ironically, only our evil deities have apostrophes in their names. Twisted Evil Rolling Eyes
Back to top
View user's profile Send private message
Asmodae



Joined: 07 Jan 2005
Posts: 55

PostPosted: Thu Jun 29, 2006 19:01    Post subject: Reply with quote

We use a bit different system for dealing with this. I didn't like encode/decode special characters since it loops, and that's never good for performance. Basically we wrap strings with double quotes instead of single quotes, so the single quote becomes part of the string. Haven't had any issues with it yet.

To do this requires some trickery:

Open your mod in the toolset and place down a placeable object. If you have a utility area that noone enters that's great use it, if not a waypoint or something invisible will work. Give it a unique name, and go to the variables properties tab. Add a string and give that string a value of one individual double quote character. Name the variable something like "quote".

Now make a function in your SQL library called GetQuote() (or shorter if you prefer).

Code:

string GetQuote()
{
   return GetLocalString(
      GetObjectByTag("QuoteHoldingObject"),
      "quote");
}


then instead of wrapping your strings like this:

... ' " + "S'ierhan" + " ' ...

you do this:
..." + GetQuote() + "S'ierhan" + GetQuote() + " ...

This means that stringVar can contain as many single quotes as you want and it will process through MySQL just fine. Yay, for apostrophied names again. Also this means you do not need to use encode/decode anymore. Very Happy

an alternative utility function might be something like:

Code:

string EnQuote(string sStuff)
{
   string sQuote = GetLocalString(
      GetObjectByTag("QuoteHoldingObject"),
      "quote");

   return sQuote + sStuff + sQuote;
}


so usage becomes
... " + EnQuote("S'ierhan") + " ...

Good luck!

- Asmodae
_________________
Nepenthe - An NWN2 Persistant World, coming to a planet near you. http://www.nepentheonline.com
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Smolin



Joined: 10 Jul 2005
Posts: 4

PostPosted: Thu Jun 29, 2006 19:21    Post subject: Reply with quote

Wow. Shocked That's a really neat trick! Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Scripts and Modules All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group