Help: Omitting quotes from SQL Queries.

taocairns nospam at earthlink.net
Wed Jan 8 13:21:57 CET 2003


keithk wrote:
> Hi again,
> 
> I have tried some of your suggestions when using a variable for a
> column name:
> 
> Using a correct Column name:
> 
> fileName = "Hello World's"
> for fileName in fileNameList:
>       cursor.execute("""SELECT fileSize FROM DBServer.database a where
>       a.ColumnName = "%s" """" % fileName)
>       fileSizes = cursor.fetchall()
> 
> Above returns an 'invalid column name' error.
> 
> fileName = "Hello World's"
> for fileName in fileNameList:
>       cursor.execute("SELECT fileSize FROM DBServer.database a where
>       a.ColumnName= %s " % fileName)
>       fileSizes = cursor.fetchall()
> 
> Above gives an "incorrect syntax near" error.
> 
> 
> fileName = "Hello World's"
> for fileName in fileNameList:
>       cursor.execute("SELECT fileSize FROM DBServer.database a where
>       a.ColumnName= '%s' " % (fileName),)
>       fileSizes = cursor.fetchall()
> 
> I also tried the above with various qoutes, eg:
> 
> fileName = "Hello World's"
> for fileName in fileNameList:
>       cursor.execute("""SELECT fileSize FROM DBServer.database a where
>       a.ColumnName = '"%s" """ % (fileName),)
>       fileSizes = cursor.fetchall()
> 
> 'Invalid column name' is returned, this is returned whenever I use the
> triple qoutes, """.
> 
> If I use string.replace() method and replace the ' with " then I receive
> no errors but no data is retrived from the DB as "Hello World's" does
> not equal 'Hello World"s' thus no match is made in the DB.
> 
> Daniel,
> 
> Could you give me a little more detail about the
> 
> execute ("SELECT ...",[fileName]) appraoch as I am not familiar with
> the syntax.
> 
> Thanks to all for replying.
> 
> --
> Posted via http://dbforums.com
I'm out of my depth on SQL, but it looks like cursor.execute() is 
expecting more than one argument?

I suggest you play with the parameter string in the interactive window 
to see just what it is that you are feeding SQL.





More information about the Python-list mailing list