Meesterslager
Joined: 14 Sep 2008 Posts: 4
|
Posted: Sun Oct 05, 2008 11:36 Post subject: How to write and Read from the database? |
|
|
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. |
|
TroveLord
Joined: 22 Nov 2006 Posts: 136 Location: Italy
|
Posted: Sun Oct 05, 2008 12:37 Post subject: |
|
|
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. |
|