dbTalk Databases Forums  

escaping table/field names and values

comp.databases comp.databases


Discuss escaping table/field names and values in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jason S
 
Posts: n/a

Default escaping table/field names and values - 04-27-2007 , 11:13 AM






I've googled for this but can't seem to find an answer that looks
reliable & is not product-specific.

What is an adequate method of escaping table/field names and values in
SQL that would work on any major database type? e.g. if I am getting a
field name and value from an untrusted source (e.g. an http query from
another computer), and I want to use it as part of an SQL query, how
should I modify the string so it works properly?

e.g. in C if I do _snprintf(buf, bufsz, "select * from %s T where T.`
%s` = '%s';", table_name, table_name, field_name, field_value), this
will have problems if field_name contains a back-quote or field_value
contains a single-quote. (and are these the only characters that are
problematic?) I can lookup the answer for MySQL which I am using right
now, but if I want to make my software work w/ other databases I'm not
sure what the right general answer is.


Reply With Quote
  #2  
Old   
Jason S
 
Posts: n/a

Default Re: escaping table/field names and values - 04-27-2007 , 11:29 AM






On Apr 27, 12:13 pm, Jason S <jmsa... (AT) gmail (DOT) com> wrote:
Quote:
e.g. in C if I do _snprintf(buf, bufsz, "select * from %s T where T.`
%s` = '%s';", table_name, table_name, field_name, field_value), this
typo: that should have been
_snprintf(buf, bufsz, "select * from %s T where T.`> %s` = '%s';",
table_name, field_name, field_value);



Reply With Quote
  #3  
Old   
Ed Prochak
 
Posts: n/a

Default Re: escaping table/field names and values - 04-27-2007 , 01:51 PM



On Apr 27, 12:13 pm, Jason S <jmsa... (AT) gmail (DOT) com> wrote:
Quote:
I've googled for this but can't seem to find an answer that looks
reliable & is not product-specific.

What is an adequate method of escaping table/field names and values in
SQL that would work on any major database type? e.g. if I am getting a
field name and value from an untrusted source (e.g. an http query from
another computer), and I want to use it as part of an SQL query, how
should I modify the string so it works properly?

e.g. in C if I do _snprintf(buf, bufsz, "select * from %s T where T.`
%s` = '%s';", table_name, table_name, field_name, field_value), this
will have problems if field_name contains a back-quote or field_value
contains a single-quote. (and are these the only characters that are
problematic?) I can lookup the answer for MySQL which I am using right
now, but if I want to make my software work w/ other databases I'm not
sure what the right general answer is.
I do not think there is a general answer, but i think there is a more
generic version by using the AS so
"select * from %s AS T where T.%s = '%s';", table_name, field_name,
field_value

and I don't think you want to single quote the column name do you?

HTH,
Ed



Reply With Quote
  #4  
Old   
Jason S
 
Posts: n/a

Default Re: escaping table/field names and values - 04-30-2007 , 04:11 PM



On Apr 27, 2:51 pm, Ed Prochak <edproc... (AT) gmail (DOT) com> wrote:
Quote:
I do not think there is a general answer, but i think there is a more
generic version by using the AS so
"select * from %s AS T where T.%s = '%s';", table_name, field_name,
field_value

and I don't think you want to single quote the column name do you?

HTH,
Ed
thanks. Mysql uses backquotes for column name, what do other db's use?




Reply With Quote
  #5  
Old   
Lee Fesperman
 
Posts: n/a

Default Re: escaping table/field names and values - 05-04-2007 , 02:30 AM




Jason S wrote:
Quote:
Mysql uses backquotes for column name, what do other db's use?
ANSI SQL uses double quotes (") for entity names. MySQL will accept
double quotes if you choose ANSI mode.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)



Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.