我是编码的初学者。我正在尝试制作一个后端数据库,用于存储来自tkinter gui的条目。如下所示,我正在创建一个包含5个条目(食品,运输,保险,分期付款等)的数据库,每次我需要为INSERT和UPDATE函数重复此列表时。如果我需要创建另外5或10类数据,则进行编辑将很繁琐。
因此,有一种方法可以创建一个主列表,然后将其链接到sql中的那些函数?代码看起来应该如何,因为sql命令都在“”中
import sqlite3
def expensedata():
con = sqlite3.connect("expense.db")
c = con.cursor()
c.execute("CREATE TABLE IF NOT EXISTS expense (id INTEGER PRIMARY KEY,\
food real, transport real, insurance real, installments real, others real)")
con.commit()
con.close()
def adddata(food, transport, insurance, installments, others):
con = sqlite3.connect("expense.db")
c = con.cursor()
c.execute("INSERT INTO expense VALUES (NULL, ?,?,?,?,?)", (food, transport, insurance, installments, others))
con.commit()
con.close()
def updatedata(id, food="", transport="", insurance="", installments="", others=""):
con = sqlite3.connect("expense.db")
c = con.cursor()
c.execute("UPDATE expense SET food=?, transport=?, insurance=?, installments=?, others=? WHERE id=?",
(food, transport, insurance, installments, others, id))
con.commit()
con.close()
expensedata()