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 
 
Re: Possible replacement for SQLEncodeSpecialCharacters()
Goto page 1, 2  Next
 
Post new topic   Reply to topic    nwnx.org Forum Index -> Scripts and Modules
View previous topic :: View next topic  
Author Message
Asmodae



Joined: 07 Jan 2005
Posts: 55

PostPosted: Fri Jan 07, 2005 8:02    Post subject: Re: Possible replacement for SQLEncodeSpecialCharacters() Reply with quote

This info should still be relevant.

In setting up the database for Exodus, we have some pretty large strings many of which contain single quotes as appostrophies for dynamic conversations and such. I was leary about depending on a coversion routine that cycles through long character sets doing a find and replace function to AND from the database.

So I did a little digging, nwscript has no escape characters so that was out. What I found was intriguing. Take an object, the module for instance, but since a lot of things get stored on the module, a secondary hidden object is preferred. Lets use for this example an object with the tag DOUBLE_QUOTE , catchy right? Open up the toolset and manually set a variable on this object called "doublequotes" or some such. Assign it a single character value, one solitary doublequote.

At the beginning of your scripts create a global variable, I know its generally bad practice but its for performance reasons. Smile
Code:

string DQ = GetLocalString(GetObjectByTag("DOUBLE_QUOTE),"doublequote");


Now modify your SQL string converter for passing queries to the database into something like:

Code:

string SQLEncodeSpecialCharacters(string sString)
{
    return (DQ + sString + DQ) ;
}


This handily encases the passed string in double quotes and is brilliant for concatenating SQL queries with pesky single quotes in them. I found this little tidbit very usefull even though it is a bit hacky.

Thanks again folks.

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
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Fri Jan 07, 2005 23:45    Post subject: Reply with quote

Ah, very interesting! Recently, I was thinking about was to replace that function myself. This information might come in handy! Thanks.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Senalaya



Joined: 29 Dec 2004
Posts: 82
Location: Germany

PostPosted: Sat Jan 08, 2005 1:30    Post subject: Reply with quote

Sounds good, but might be a bit too 'techy' for some people. What about declaring a global string variable in the 'aps_include' and setting it to the double-quote char in SQLInit() with a "SELECT CHAR(34)"?

Last edited by Senalaya on Sat Jan 08, 2005 1:37; edited 1 time in total
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Sat Jan 08, 2005 1:36    Post subject: Reply with quote

I was thinking along those lines, yes. Probably I would just return a readily made up string with a special nwnx odbc function.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Senalaya



Joined: 29 Dec 2004
Posts: 82
Location: Germany

PostPosted: Sat Jan 08, 2005 1:37    Post subject: Reply with quote

Sorry, edited inbetween ...
Back to top
View user's profile Send private message
Asmodae



Joined: 07 Jan 2005
Posts: 55

PostPosted: Sat Jan 08, 2005 4:43    Post subject: Reply with quote

Glad it could be of use. To be honest though I didn't discover the object trick. I found it on the scripting forums, and it turned out to be a huge boon for our database on Exodus... so I figured I ought to share with the database people.
Wink

quick question, any easy way to back up the database files? (using MySQL)

Just copy the data files to a new location seems the easiest for now, but curious if there are other methods.
_________________
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
Manuel



Joined: 30 Dec 2004
Posts: 51

PostPosted: Sat Jan 08, 2005 5:27    Post subject: Reply with quote

I've always used mysqldump and set it to run via cron jobs. mysqldump should be in your /bin directory.

Years ago (99?), I wrote a Perl script to loop through all the dbs and tables and so forth. I was going to Open Source it but someone else beat me to it. Here's the project:
http://freshmeat.net/projects/mysqlbackup/

Here's one in PHP:
http://freshmeat.net/projects/phpmysqlbackup/

And mysqldump itself:
http://dev.mysql.com/doc/mysql/en/mysqldump.html

There's also another MySQL native utility called hotcopy or some such.
Back to top
View user's profile Send private message
JeroenB



Joined: 31 Dec 2004
Posts: 228
Location: Netherlands

PostPosted: Sat Jan 08, 2005 10:25    Post subject: Reply with quote

Why not calling the default MySQL functions for those special escape characters. I am pretty sure they know how to implement it fast and compatible with their database.
Back to top
View user's profile Send private message Visit poster's website
Senalaya



Joined: 29 Dec 2004
Posts: 82
Location: Germany

PostPosted: Sat Jan 08, 2005 14:19    Post subject: Reply with quote

Question:

Is the "SELECT CHAR(34)" SQL command supported under MySQL and MS Access?
I know that MS-SQL supports it and it would be the most simple way, with the current NWNX code, to get the " char into the game as variable.
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Sat Jan 08, 2005 17:17    Post subject: Reply with quote

JeroenB wrote:
Why not calling the default MySQL functions for those special escape characters. I am pretty sure they know how to implement it fast and compatible with their database.


Does an equivalent function exist for ODBC ? Since you are the one who unified the mysql and odbc layer, and since everything needs to be compatible with both, you have the great honor of finding this out Twisted Evil
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
JeroenB



Joined: 31 Dec 2004
Posts: 228
Location: Netherlands

PostPosted: Sat Jan 08, 2005 19:49    Post subject: Reply with quote

Shocked Did I mention anything?? Rolling Eyes

Alright, I will have a look for it.
Back to top
View user's profile Send private message Visit poster's website
Senalaya



Joined: 29 Dec 2004
Posts: 82
Location: Germany

PostPosted: Sat Jan 08, 2005 20:18    Post subject: Reply with quote

I did some tests here already....

- CHAR(43)/CHR(34) is not transparent, most DBs and the ODBC standard support the latter, but MS-SQL insists on CHAR(), even via ODBC.

- Same goes for the use of double-quotes to encapsulate strings with single quotes. It's not transparent.

Another way could be to replace single ' in strings with '' (2x single quote), MS-SQL, MS-SQL via ODBC and MS-Access support that, but I'm not sure about MySQL and ODBC in general.

Example:

Character name: Emanuel d'Test
SQL string example: SELECT * FROM table WHERE name='Emanuel d''Test'
NWScript: sSQL = "SELECT * FROM table WHERE name='" + SQLEnc(name) + "'"; (SQLEnc() doubles the single quote in that name)

You don't have to worry about decoding it again, since the name will be stored and retrieved with only the single quote = Emanuel d'Test.
Back to top
View user's profile Send private message
dguntner



Joined: 31 Dec 2004
Posts: 116

PostPosted: Sun Jan 09, 2005 3:44    Post subject: Reply with quote

Except that they're talking about testing with MySQL, and you tested MS-SQL, which is not the same thing... Laughing

--Dave
Back to top
View user's profile Send private message
Roland Rosier



Joined: 06 Mar 2005
Posts: 5

PostPosted: Mon Mar 21, 2005 23:39    Post subject: SQLEncodeSpecialChars and SQLDecodeSpecialChars mapping Reply with quote

When I started writing scripts to pass Character names in and out of MySQL, I wrote a function which replaced all single quotes with two single quotes. In this fashion, the Character names in my database end up matching the character names directly used in the game.

That means I can retrieve a character name from the database and compare it against a character name from the game.

Now, I believe that the SQLEncodeSpecialChars and SQLDecodeSpecialChars have a mapping multiplicity issue. SQLEncodeSpecialChars replaces all single quotes with ~. At the same time, any ~ characters used in character names are left as ~. This means that if a character name is retrived from the database and run through SQLDecodeSpecialChars, any ~ characters are turned into single quotes. If the original character was a ~, this resulting string will not test for equality against the original character name because the ~ will now be a '.

Hence, the mapping of SQLEncodeSpecialChars is not 1:1.

Would it not be a better idea if SQLEncodeSpecialChars just replaces all instances of single quotes with two single quotes, thereby avoiding this problem?

Roland Rosier.
Back to top
View user's profile Send private message
dguntner



Joined: 31 Dec 2004
Posts: 116

PostPosted: Tue Mar 22, 2005 9:42    Post subject: Reply with quote

I don't think MySQL would like two ' characters in a row, but I've never tested it. It uses a single quote to wrap around things that it's searching for. I'm not sure how it would take when given something like

select * from pwdata where player = 'some''string';

(that's two single quotes in the middle, not a double quote).

--Dave
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
Goto page 1, 2  Next
Page 1 of 2

 
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