Python开发客户管理应用遇sqlite3.InterfaceError:参数类型不支持
解决sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type错误
错误原因很直接:uuid.uuid4()生成的是UUID对象,SQLite无法直接识别并存储这种类型,必须先转换成字符串格式才能插入数据库。
修改方案
把生成的UUID对象转为字符串,推荐在生成ID时直接转换,代码逻辑更清晰:
修改后的完整代码:
import uuid import time import sqlite3 # 假设connection是已建立的数据库连接 connection = sqlite3.connect('your_database.db') def customer_creation_process(): # 初始化客户创建流程,收集用户输入的客户信息 customer_forename = input("请输入客户的名字:").strip() customer_surname = input("请输入客户的姓氏:").strip() customer_dob = input("请输入客户的出生日期,格式为:22/07/2002:").strip() print("生成唯一客户ID...") # 将UUID对象转为字符串 customer_id = str(uuid.uuid4()) print("正在将客户信息插入系统...") insert_customer_details(customer_id, customer_forename, customer_surname, customer_dob) time.sleep(2) print(f"已记录以下客户信息:{customer_id}, {customer_forename}, {customer_surname}, {customer_dob}") # 将客户信息插入数据库的customer表 def insert_customer_details(customer_id, customer_forename, customer_surname, customer_dob): """将客户信息插入数据库""" cursor = connection.cursor() cursor.execute("INSERT INTO customer VALUES (?, ?, ?, ?)" , (customer_id, customer_forename, customer_surname, customer_dob)) connection.commit() return
额外说明
确保你的customer表中,存储customer_id的字段类型是TEXT(SQLite没有专门的UUID类型,用TEXT存储是标准做法)。如果你的字段定义为其他类型(比如INTEGER),则需要调整字段类型,或者换用自增整数作为客户ID。
内容的提问来源于stack exchange,提问作者addoadil
相关产品推荐
相关产品推荐

