开发者

DB-API SQLite INSERT statement throws InterfaceError in Python 2.7

开发者 https://www.devze.com 2023-02-06 06:41 出处:网络
Given a SQLite table I\'ve created, with the field in question being type TEXT, the following fails: hsh = hashlib.sha1(\"\".join(some_list)).hexdigest()

Given a SQLite table I've created, with the field in question being type TEXT, the following fails:

hsh = hashlib.sha1("".join(some_list)).hexdigest()

db_setup(hsh)

I receive the error:

InterfaceError: Error binding parameter 2 - probably unsupported type.

def db_setup(my_hash, oavals)
    to_insert = (my_hash,)
    ('INSERT INTO position VALUES \
    (null, ?, ?, null, ?, ?, ?, ?, ?)',(0, 0, to_insert,
    oavals["a"], oavals["b"],
    oavals["c"], oavals["d"]))

If I substitute a manual int or string (e.g. 57, or "hello" for to_insert in the db_setup def, it works fine, which leads me to believe that it's tripping over the hash, for 开发者_Python百科some reason. I feel as if I'm missing something obvious here.


Table schema:

'CREATE TABLE position \

(id INTEGER PRIMARY KEY, position INTEGER, displayline INTEGER, \

header TEXT, digest TEXT, conkey TEXT, consecret TEXT, \

acckey TEXT, accsecret TEXT)'


It is failing because to_insert is a tuple, not the value you wish to insert.

Try changing that first line to:

to_insert = my_hash

Or better yet, just put my_hash into your parameter list.

Here's an example:

>>> hsh = '49cb6536afc7e4a4b3a94eb493aae4d52b8f6a60'
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
>>> c.execute("CREATE TABLE t (i TEXT);")
>>> c.execute("INSERT INTO t VALUES (?)", (hsh,)) # tuple works
<sqlite3.Cursor object at 0x011A35E0>
>>> c.execute("INSERT INTO t VALUES (?)", ((123,),)) # tuple within tuple doesn't
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
0

精彩评论

暂无评论...
验证码 换一张
取 消