Raised This Month: $51 Target: $400
 12% 

[SNIPPET] Escape a char from a String (With SQL support)


Post New Thread Reply   
 
Thread Tools Display Modes
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 04-02-2013 , 10:00   Re: [SNIPPET] Escape a string for MySQL and SQLite
Reply With Quote #11

Nice. This doesn't even have to be used with SQL. It could also be a general purpose function. Perhaps expand it to allow the user to choose which escape character to use.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 04-02-2013 at 10:01.
rhelgeby is offline
Send a message via MSN to rhelgeby
API
Veteran Member
Join Date: May 2006
Old 04-03-2013 , 11:31   Re: [SNIPPET] Escape a string for MySQL and SQLite
Reply With Quote #12

It's a cool idea but obviously the driver is preferred. Also, as a suggestion: Don't check if characters are already escaped, just escape them anyway. It's pretty common to have other escaped information in the string so you don't want to make any assumptions about what is fair game for escaping what what isn't. For example:

x = 'test\'ing';

That string should become

x = 'test\\\'ing';
__________________
API is offline
Send a message via AIM to API
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-03-2013 , 20:03   Re: [SNIPPET] Escape a string for MySQL and SQLite
Reply With Quote #13

Quote:
Originally Posted by asherkin View Post
It's idiotic to use this, if you need to insert text into a database, you're going to have a connection.
The driver-level functions are designed to escape exactly what's required.
And just as importantly, since you didn't mention it: The standard SQL way of escaping is to replace ' with ''.

MySQL is more involved than that but it still supports using '' to escape '. I can't honestly say whether SQLite supports that.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-03-2013 at 20:05.
Powerlord is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 04-04-2013 , 08:08   Re: [SNIPPET] Escape a char from a String (With SQL support)
Reply With Quote #14

I updated the snippet, so you can generally escape a string from a char with a given escaper. And also the snippet is a lot of smaller.
__________________
dordnung is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 04-04-2013 , 13:27   Re: [SNIPPET] Escape a char from a String (With SQL support)
Reply With Quote #15

Code is clean and well formatted. However there are two improvements:
  • Move call to strlen() outside the loop to make the snippet as efficient as possible. Even calls to Format to concatenate strings are unnecessary if you work with strings as arrays. But that isn't a big deal.
  • When you call EscapeString multiple times in the SQL escaping functions, use the output from the last one as the input in the next one. Otherwise the last result will be overwritten.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 04-04-2013 at 13:31.
rhelgeby is offline
Send a message via MSN to rhelgeby
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:59.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode