InterfaceError:错误绑定参数0 - 运行我的django脚本时可能不支持的类型

问题描述 投票:1回答:1

我有以下功能

def get_id(entityName, text):  

"""Retrieve an entity's unique ID from the database, given its associated     text.
If the row is not already present, it is inserted.
The entity can either be a sentence or a word."""  

    tableName = entityName + 's'  
    columnName = entityName
    cursor.execute('SELECT rowid FROM ' + tableName + ' WHERE ' + columnName + ' = %s', (text,))
    row = cursor.fetchone()

    if row:
        return row[0]
    else:
        cursor.execute('INSERT INTO ' + tableName + ' (' + columnName + ') VALUES (?)', (text,))
        return cursor.lastrowid  

什么时候这个方法被调用它产生这个错误

 cursor.execute('SELECT rowid FROM ' + tableName + ' WHERE ' + columnName + ' = ?', (text,))
InterfaceError: Error binding parameter 0 - probably unsupported type.

目前这个错误正在产生,当我在django中运行它,否则它的工作正常。可能是什么原因?

python django python-2.7 sqlite cursor
1个回答
1
投票

这里我的参数0(文本)的类型是<type 'unicode'>,数据库中的列数据类型是text type所以错误

InterfaceError: Error binding parameter 0 - probably unsupported type.

很明显,因为参数0不符合数据库列的类型 我之前没有得到这个,因为我从另一个来源得到这个。 但是在得到它之后不是文本类型我必须在文本类型中转换它 有点像str(text) 它的工作就像一个魅力吧

© www.soinside.com 2019 - 2024. All rights reserved.