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 over SQLFetch()

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
darwinscusp



Joined: 19 Jan 2005
Posts: 19

PostPosted: Wed Jan 19, 2005 5:06    Post subject: looping over SQLFetch() Reply with quote

I'm attempting to find a way to loop over multiple row returns from a query.

Ok, this is probably a ridiculously simple question to answer, but I can't seem to figure it out, or find it posted anywhere.

If I build a SELECT query, that returns multiple rows, how do I access the multiple rows? So far, I've got the following process, but it doesn't seem to return more than one row.


Code:

string sSQL = "SELECT * from spell_table;";
SQLExecDirect(sSQL);
while (SQLFetch())
{
    int nSpellID = StringToInt(SQLGetData(1));
    int nSPower  = StringToInt(SQLGetData(2));
    SendMessageToPC(oPC,"Found " + StringToInt(nSpellID));
}




Any suggestions? I'm basically trying to find a way to iterate over each row that's returned from the query.

Thanks!
Back to top
View user's profile Send private message
Lokey



Joined: 02 Jan 2005
Posts: 158

PostPosted: Wed Jan 19, 2005 8:26    Post subject: Reply with quote

Wasn't SQLGetData() an older version function? Don't have the standard distribution of the db functions in NWScript (made some changes), but remember reading something about that in the comments of the database script set.

Thinking this should work as you expect (you're right about SQLFetch() changing rows and the other function changing columns):
Code:

string sSQL = "SELECT * from spell_table;";
SQLExecDirect(sSQL);
while (SQLFetch())
{
    int nSpellID = StringToInt(SQLGetField(1));
    int nSPower  = StringToInt(SQLGetField(2));
    SendMessageToPC(oPC,"Found " + StringToInt(nSpellID));
}


Edit: oh, how large is the table of information you're trying to access?
_________________
Neversummer PW NWNx powered mayhem Wink
Back to top
View user's profile Send private message
darwinscusp



Joined: 19 Jan 2005
Posts: 19

PostPosted: Thu Jan 20, 2005 0:58    Post subject: Reply with quote

It's not particularly large. I think it's 807 rows max. It's just a custom spell-tracking list, that contains various other data (spell power level, etc) that I use for a PW's custom rest system. After looking around, I'm guessing I have a lingering 'limit 1' in the query or something stupid.

Thanks for taking a look. I'll post my findings later this evening.
Back to top
View user's profile Send private message
Mr-XXS



Joined: 14 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 3:47    Post subject: Reply with quote

I've had some problems with long results to requests. In fact it seems that the result max length is around 400-450 rows. So I suggest you to limit your request by using : "LIMIT 0,300", "LIMIT 300,300" etc... So you will have 300row's long results.
Back to top
View user's profile Send private message
darwinscusp



Joined: 19 Jan 2005
Posts: 19

PostPosted: Thu Jan 20, 2005 4:40    Post subject: Reply with quote

Ok, after further examination, I'm even more stuck. I ran the query manually, and in my test case, it returns 9 rows. However, the code is still only processing one row. Here's the actual code:

Code:
    string sSQL = "SELECT power,total_uses,spell_tracking.spell_id FROM " +
                  "spell_tracking,spell_power WHERE " +
                  "spell_tracking.spell_id=spell_power.spell_id AND " +
                  "spell_tracking.pc_id='" + sPCID + "'" +
                  "ORDER BY power;";
    SQLExecDirect(sSQL);
    float fPower;
    while(SQLFetch() == SQL_SUCCESS)
    {
        fPower = StringToFloat(SQLGetData(1));
        string sUses = SQLGetData(2);
        string sSpellID = SQLGetData(3);
        string sMessage = "Removing " + sSpellID + ". " + FloatToString(fSPToRestore) + ".";
        SendMessageToPC(oPC,sMessage);
        DeleteOneSpellInstance(sPCID,sSpellID,sUses);
        fSPToRestore -= fPower;
        if (fSPToRestore <= 0.0f) return;
    }


Any ideas?
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Thu Jan 20, 2005 9:22    Post subject: Reply with quote

Mr-XXS wrote:
I've had some problems with long results to requests. In fact it seems that the result max length is around 400-450 rows. So I suggest you to limit your request by using : "LIMIT 0,300", "LIMIT 300,300" etc... So you will have 300row's long results.


There is no such limit, unless it is a bug.

Code:

Any suggestions? I'm basically trying to find a way to iterate over each row that's returned from the query.


As always in cases like this, what does the log say ?
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Primogenitor



Joined: 08 Jan 2005
Posts: 88

PostPosted: Fri Feb 04, 2005 14:35    Post subject: Reply with quote

I seem to have hit a limit as well Sad . Basically, I have a table that is a replicate of the portraits.2da file (1099 lines long, 7 columns), and I have the following NWscript to access it:
Code:

string SQL = "SELECT row, BaseResRef FROM cached2da_portraits WHERE (BaseResRef <> '' )";
        SQLExecDirect(SQL);
        while(SQLFetch() != SQL_ERROR)
        {
            PrintString(SQLGetData(1)+" "+SQLGetData(2));
        }
        PrintString("END OF FILE");


So you would expect that to return 1065 lines, ending with

1065 el_f_17_
END OF FILE

However, instead I get:

599 ****
600 PLC_C10_

at the end in nwserverlog1.txt and

o Sent response (8 bytes): 598¬****
o Sent response (8 bytes): 599¬****
o Sent response (12 bytes): 600¬PLC_C10_
o Sent response (15 bytes): 601¬cat_mpanth_

at the end of nwnx_odbc.txt It could be a script TMI error however, I dont know because Im at work so its running just on the dedicated server (windows), NWNX version V.2.6.1 ODBC plugin version V.0.9.2.3 (SQLite DB). I will try doing several queries using LIMIT to break it down into blocks of 500 and see if that helps.

EDIT: Okay, this code seems to work:
Code:

void DoOutput(int i=0)
{
        string SQL = "SELECT row, BaseResRef FROM cached2da_portraits WHERE (BaseResRef <> '' ) LIMIT "+IntToString(i)+" , "+IntToString(500);
        SQLExecDirect(SQL);
        int nCounter;
        while(SQLFetch() != SQL_ERROR)
        {
            PrintString(SQLGetData(1)+" "+SQLGetData(2));
            nCounter++;
        }
        if(nCounter < 500)
            PrintString("END OF FILE");
        else
            DelayCommand(0.01, DoOutput(i+500));
}

Looks like it was a script TMI error after all.
Back to top
View user's profile Send private message
JeroenB



Joined: 31 Dec 2004
Posts: 228
Location: Netherlands

PostPosted: Fri Feb 04, 2005 18:12    Post subject: Reply with quote

This could be the scripting limit of NWN. There is a maximum number of operations that may be performed in one script. I think you have reached that that number, as > 1000 lines results in way to many operations. Splitting this up with LIMIT will indeed solve that problem.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related 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