View previous topic :: View next topic |
Author |
Message |
earlsignet
Joined: 16 Apr 2007 Posts: 6
|
Posted: Mon Apr 16, 2007 13:44 Post subject: NWNX Problem? |
|
|
Hi,
Iv been working with NWNX for a while and iv only recently run into a problem.
I wasnt sure where to post this as I believe it covers both scripting, nwnx and nwnx4 development.
My question is about how NWNX deals with results and queries. If i were to make a function that checks the database for a result and returns true or false, and then embeded that function within a while statement which loops through a different set of records, does NWNX return incorrect or incomplete results.
I dont think im explaining this properly so ill try and make an expample:-
Code: |
#include "nwnx_sql"
void main()
{
int player_id = 2;
object player = SOMEWAYOFGETTINGYOURPLAYER;
string sSQL = "SELECT resource_id FROM storage WHERE player_id='" + IntToString(player_id) + "' AND resource_type='W'";
SQLExecDirect(sSQL);
while(SQLFetch() == SQL_SUCCESS)
{
int r_id = SQLGetData(1);
if(GetHasSkill(player_id, r_id)==TRUE)
{
int doesntmatter = 0;
}
SendMessageToPC(player, "Checked: " + IntToString(r_id));
}
}
int GetHasSkill(int player_id, int resource_id)
{
string sSQL = "SELECT resource_name FROM resources WHERE player_id='" + IntToString(player_id) + "' AND resource_id='" + IntToString(resource_id) + "'";
SQLExecDirect(sSQL);
if(SQLFetch() == SQL_SUCCESS)
{
return TRUE;
}
else
{
return FALSE;
}
}
|
The above code is the concept i was using.
Table 1 named storage would have say...4 rows that could be fetched.
Table 2 named resources would have only 1 row to fetch.
The problem is, the while loop running on Table 1 doesnt go to the next row at the end of the first pass. Instead it just thinks its done because the lookup on Table 2 only returns one result.
SO, i take it that when it makes the second query on the function, it replaces the current NWNX resultset with the new result set. Therefore the above script would not work. Correct?
If im right, is there anything that can be done about it my end or is there some clever feature of NWNX4 planned that will solve problems with multiple resultsets being executed at the same time?
Any input would be fantastic. Thanks
Mike. |
|
Back to top |
|
|
Grinning Fool
Joined: 12 Feb 2005 Posts: 264
|
Posted: Mon Apr 16, 2007 17:21 Post subject: |
|
|
You are correct, in that queries cannot be nested. There's no simple way to resolve this that I know of through NWNX.
However in many cases, a join can be performed in the SQL statement that will have the same effect (and will run faster, since it uses the SQL engine to do the work).
In your example:
SELECT resource_id FROM storage WHERE player_id=x and resource_type'W'
SELECT resource_name FROM resources WHERE player_id=X AND resource_id=Y
Can be combined to:
SELECT b.resource_id, b.resource_name
FROM storage a, resources b
WHERE a.player_id = x
AND a.resource_type = 'W'
AND b.player_id = a.player_id
AND b.resource_id = a.resource_id
This will return a list of resource(skills?) that the PC has, instead of requiring you to loop through it in script. _________________ Khalidine, a NWN2 persistent world
Looking for volunteers. |
|
Back to top |
|
|
earlsignet
Joined: 16 Apr 2007 Posts: 6
|
Posted: Mon Apr 16, 2007 18:55 Post subject: |
|
|
Thats a good point.
Whenever coding with NWNX i always forget im dealing with the MYSQL database and dont always remember i can do similar database query's that i would use in PHP.
That seems to be a much better way of doing it, thanks for the suggestion. I will do some models and see if your solution is viable in the system it is required it.
|
|
Back to top |
|
|
earlsignet
Joined: 16 Apr 2007 Posts: 6
|
Posted: Tue Apr 17, 2007 10:31 Post subject: |
|
|
Thankyou very much.
I managed to sort my database out to I can make better use of it with more common MySQL statements. Seems to be working fantasticly.
I do have one question though.
In working with MySQL i'v never tried a statement that looks up multiple rows returned in one resultset.
Say the skills table for example contained entries for skills and one column was called "NAMES" and there were two rowes i need to return in one resultset that related to two seperate values from one record in another table. Is there a way of doing this without referencing to the same table twice:
SELECT b.resource_id, b.resource_name, c.resource_name
FROM storage a, resources b, resources c
WHERE a.player_id = x
AND a.resource_type = 'W'
AND b.player_id = a.player_id
AND b.resource_id = a.resource_id
AND c.resource_id = a.resource_id
Iv got a function like the above working but im not sure if thats the only way of doing it. |
|
Back to top |
|
|
Grinning Fool
Joined: 12 Feb 2005 Posts: 264
|
Posted: Tue Apr 17, 2007 16:03 Post subject: |
|
|
You're looking to get values from two different rows in the same table, as two different columns in the resultset? If that's correct, then the only way I know of is to reference the table twice. _________________ Khalidine, a NWN2 persistent world
Looking for volunteers. |
|
Back to top |
|
|
earlsignet
Joined: 16 Apr 2007 Posts: 6
|
Posted: Tue Apr 17, 2007 16:13 Post subject: |
|
|
Thats correct and thats what I have.
I thought it made sense.
Thankyou for the confirmation. |
|
Back to top |
|
|
|