我写了一个python代码来保存数据,并将其保存到sqlite数据库中,这是我的错误:属性错误:"tuple"对象没有属性"Personal_id"

rslzwgfq  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(85)
import sqlite3

conn = sqlite3.connect('db.db')

c = conn.cursor()

def log_in(patient):
    with conn:
        c.execute("""INSERT INTO patient_office_syndrome VALUES 
        (?, ?, ?, ?, ?, ?, ?, ?)""", (patient.Personal_id,patient.first_name, patient.last_name, patient.age, patient.phone, patient.gender, patient.weight, patient.height))

patient_1 = ('1409903748846541','siri','pat','25','06119433332',0,'80','190')

log_in(patient_1)

conn.commit()
conn.close()

Traceback (most recent call last): File "c:\Users\User\Desktop\db_and_admin_web\main.py", line 19, in log_in(patient_1) File "c:\Users\User\Desktop\db_and_admin_web\main.py", line 11, in log_in (?, ?, ?, ?, ?, ?, ?, ?)""", (patient.Personal_id,patient.first_name, patient.last_name, patient.age, patient.phone, patient.gender, patient.weight, patient.height)) AttributeError: 'tuple' object has no attribute 'Personal_id'

hyrbngr7

hyrbngr71#

patient的类型是什么?您问题中的语法暗示您要使用namedtuple
例如,将patient定义为:

from collections import namedtuple

Patient = namedtuple('Patient', ('personal_id', 'first_name', 'last_name', ...))
patient = Patient(42, 'John', 'Doe', ...)

然后这样使用它:

c.execute("""INSERT INTO patient_office_syndrome VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", (patient.personal_id,patient.first_name, patient.last_name, ...)

或者(可能更简单),可以将patient实现为dict。

相关问题