How to process the nested String for sybase sql statement?

Steve Holden steve at holdenweb.com
Wed Apr 4 12:34:47 EDT 2007


boyeestudio wrote:
> now,I want to insert some data to the sybase database,
> some variable such as i,j,k,name,k,j I have defined before.
> I write the sql statement here But I meet some errors,How to write this 
> nested String for sql query?
> For example:
> 
>  >>>import Sybase
>  >>> db = Sybase.connect('boyee','sa','',"test'')
>  >>> c = db.cursor()
> ......
> 
>  >>> values = "%d,%d,%d,%s,%d,%d" % (i,j,k,name,k,j)
>  >>> c.execute("'insert into 
> productinfo(productid,spid,corpid,productname,type1,
> type2) value(@value)',{'@value':values}")
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Python24\lib\site-packages\Sybase.py", line 774, in execute
>     self.description = fetcher.start(self.arraysize)
>   File "C:\Python24\lib\site-packages\Sybase.py", line 517, in start
>     return self._start_results()
>   File "C:\Python24\lib\site-packages\Sybase.py", line 645, in 
> _start_results
>     self._raise_error(Error, 'ct_results')
>   File "C:\Python24\lib\site-packages\Sybase.py", line 506, in _raise_error
>     raise exc(text)
> Sybase.Error: ct_results
> 
> Any reply is enjoyable,Thank a lot!
> 
Try instead:

     values = (i,j,k,name,k,j)
     c.execute("""insert into  productinfo
             (productid, spid, corpid, productname, type1, type2)
             values (%d,%d,%d,%s,%d,%d)""",
         values)

I haven't used the Sybase module - you may find you need to use

    %s,%s,%s,%s,%s,%s

to represent the parameterized values in the query rather than

    %d,%d,%d,%s,%d,%d

Don't forget to call

   db.commit()

to make the changes permanent.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list