python's trackback error - using pymssql -


am trying execute below code using python 2.5.2. script establishing connection , creating table, failing below error.

the script

import pymssql conn = pymssql.connect(host='10.103.8.75', user='mo', password='the_password', database='sr_wf_model') cur = conn.cursor() cur.execute('create table persons(id int, name varchar(100))') cur.executemany("insert persons values(%d, %s)", \     [ (1, 'john doe'), (2, 'jane doe') ]) conn.commit()  cur.execute("select * persons salesrep='%s'", 'john doe') row = cur.fetchone() while row:     print "id=%d, name=%s" % (row[0], row[1])     row = cur.fetchone()  cur.execute("select * persons salesrep 'j%'")  conn.close() 

the error

traceback (most recent call last):   file "connect_to_mssql.py", line 9, in <module>     cur.execute("select * persons salesrep='%s'", 'john doe')   file "/var/lib/python-support/python2.5/pymssql.py", line 126, in execute     self.executemany(operation, (params,))   file "/var/lib/python-support/python2.5/pymssql.py", line 152, in executemany     raise databaseerror, "internal error: %s" % self.__source.errmsg() pymssql.databaseerror: internal error: none 

any suggestions? plus, how read traceback error, can me understand error message? how read it? bottom up?

i think assuming regular python string interpolation behavior, ie:

>>> = "we should never '%s' when working dbs" >>> % 'this' "we should never 'this' when working dbs" 

the % operator within execute method looks normal string formatting operator more of convenience or mnemonic; code should read:

cur.execute("select * persons salesrep=%s", 'john doe')

without quotes, , work names o'reilly, , prevent sql injection per database adapter design. database adapter there -- converting python objects sql; know how quote string , escape punctuation, etc. work if did:

>>> thing_one_should_never_do = "select * table cond = '%s'" >>> query = thing_one_should_never_do % 'john doe' >>> query "select * table cond = 'john doe'" >>> cur.execute(query) 

but bad practice.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -