记账系统

jz642373 程序题 共 个子题

题目说明

创建一个简单的个人支出记账系统,帮助自己监控和管理日常开支。拟使用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()

本题为编程题,提交代码并运行通过测试点后判分。

正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
提交记录
共 条 仅展示自由练习提交
正在加载提交记录…
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧