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 
 
How to write and Read from the database?

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



Joined: 14 Sep 2008
Posts: 4

PostPosted: Sun Oct 05, 2008 11:36    Post subject: How to write and Read from the database? Reply with quote

OK i am completly new to nwn.

Installing mysql atm. (had sqlite)

How can I from a script write to a certain database row and read from a database row?


For example.

Database

Accountname Playername Online PKkills PKdeaths PVEkills PVEdeaths Totaldeaths


What i want to do.

When someone enters/ leaves set online to 1/0 when someone activates and item/placable i want to show all statistics from thos who are online, also want to show all the higest in each stat, no matter if they are online.

MY PROBLEM

How i write / read my database from the game?

Thanks in advance

meest.
Back to top
View user's profile Send private message
TroveLord



Joined: 22 Nov 2006
Posts: 136
Location: Italy

PostPosted: Sun Oct 05, 2008 12:37    Post subject: Reply with quote

It's not that hard, you must use the SQLExecDirect function to query the database and SQLFetch() to retrieve the values from the query (in case you used a SELECT)
For example.

1. Update online status. OnEnterEvent.
Code:

string account = SQLEncodeSpecialChars(GetPCPlayerName(oPC));
string cname = SQLEncodeSpecialChars(GetName(oPC));
string cdkey = GetPCPublicCDKey(oPC);
SQLExecDirect("UPDATE live_stats SET is_online = 1 WHERE account = '"+account+"' AND cdkey = '"+cdkey+"' AND cname = '"+cname+"'");

As you can see, this query sets is_online to 1 in the character row. Mind the SQLEncodeSpecialChars I used for account and cname, it converts the apostrophe to a tilde (~). That prevents that your query is screwed by an apostrophe in the character or account name.

2. Retrieve data.
Code:

string sIP, sCreated;
SQLExecDirect("SELECT ip, created FROM players WHERE account = '"+account+"' AND cdkey = '"+cdkey+"' AND cname = '"+cname+"'");
if(SQLFetch() == SQL_SUCCESS)
{
      sIP = SQLGetData(1);
      sCreated = SQLGetData(2);
}

This function retrieves the IP of the player and the date of creation of the character from the players table when account, cname and account are matching.
I used SQLExectDirect to execute the query. Right after you can see SQLFetch(), it moves the cursor the the next row of the resultset, in this case the first row. SQLGetData() will return you the value of the results. The integer parameter defines the column you want to read. So SQLGetData(1) will return the IP since I defined it as first value of the SELECT query and SQLGetData(2) will return the timestamp.

Keep in mind that you can submit any sort of query with SQLExecDirect, so you can do inner, left or right joins, drops, truncates, create tables and so on.
I hope it was all clear.
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