记账系统
类型:程序题

题目说明

创建一个简单的个人支出记账系统,帮助自己监控和管理日常开支。拟使用SQLite数据库来存储支出记录,数据库名为expenses.db,并设计一个表expenses来记录每一笔支出,表中包含三个字段:id(整型,主键自增),category(文本类型,表示支出类别),amount(实数类型,表示支出金额)。

请完善以下Python程序来实现个人支出跟踪的基本功能。

(本题无需运行通过,写入代码即可)

import sqlite3
def create_expenses_table():
    connection = sqlite3.connect('expenses.db')
    cursor = connection.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS expenses (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            category TEXT,
            amount REAL
        )
    ''')
    connection.commit()
    connection.        ①        ()

def add_expense(category, amount):
    connection = sqlite3.connect('expenses.db')
    cursor = connection.cursor()
    cursor.execute("INSERT INTO expenses (category, amount) VALUES (?, ?)", (        ②        ,         ③        ))
    connection.commit()
    connection.close()

def fetch_all_expenses():
    connection = sqlite3.connect('expenses.db')
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM expenses")
    expenses_records = cursor.        ④        ()
    for record in expenses_records:
        print(record)
    connection.close()

# 调用函数
create_expenses_table()
add_expense("Food", 25.5)
add_expense("Transport", 10.75)
fetch_all_expenses()
代码编辑器 加载中...
留空使用题目默认限制
{{resultStatus.text}}
正在加载运行记录...
乐编题库AI助手
人工客服
微信扫码联系客服
历史对话
{{ s.title || '未命名对话' }}
{{ s.created_at }}
暂无历史对话
我能帮助你什么?