首页 文章

尝试将数据插入sqlite3时出错 - 错误绑定参数0 - 可能不受支持的类型

提问于
浏览
0

我试图将数据从Kivy的textinput字段插入到sqlite3数据库,但是出现了以下问题 . 代码段

def save(self):

    conn = Database.db_connect()
    cursor = conn.cursor()

    # kivy textinput widgets are assigned variable no, name
    no = self.empid_text_input
    name = self.empname_text_input.text

    try:
        save_index_sql="INSERT INTO EmpInfo (EmpID , EmpName) VALUES (?,?)"
        conn.execute(save_index_sql, (no, name)) # Causes Error
        conn.commit()
        conn.close()
    except sqlite3.IntegrityError as e:
        print("Error: ",e)

# THROWS ERROR----> sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

数据库文件Emp.db包含以下表和结构:EmpInfo和EmpImage

  • EmpInfo CREATE TABLE EmpInfo(EmpID整数PRIMARY KEY,EmpName文本NOT NULL)

  • EmpImage CREATE TABLE EmpImage(EmpID整数PRIMARY KEY,EmpPhoto BLOB NOT NULL)

Casting gives the following results:

# conn.execute(save_index_sql,(int(no), str(name))) # RETURNS----> TypeError: int() argument must be a string, a bytes-like object or a number, not 'TextInput'
        # conn.execute(save_index_sql, (str(no), str(name))) # RETURNS----> Error:  datatype mismatch

1 回答

  • 1

    您正在尝试插入 TextInput 对象 . 您想要插入 TextInput 的文本值 .

    这也必须转换为 integer

    更改:

    no = self.empid_text_input

    至:

    no = int(self.empid_text_input.text)

相关问题