使用Python 和SQLite 数据库创建个人记账应用

在这篇博客中,我们将介绍如何使用Python和SQLite数据库创建一个简单的个人记账应用。这个应用程序允许用户记录收入和支出,并生成月度或年度报告。我们将逐步讲解代码的实现,并展示如何运行这个应用程序。

步骤

  1. 导入库:首先,我们需要导入sqlite3datetime模块。
  2. 创建数据库连接和表:创建一个数据库连接,并创建一个名为transactions的表,用于存储交易记录。
  3. 定义函数:定义两个函数,一个用于向数据库中添加交易记录,另一个用于生成月度或年度报告。
  4. 主程序:提供一个简单的命令行界面,允许用户选择记录收入、记录支出、生成月度报告、生成年度报告或退出程序。

代码解析

  1. 导入库

    import sqlite3
    from datetime import datetime
    

  2. 创建数据库连接和表

    # 创建数据库连接
    conn = sqlite3.connect('finance.db')
    c = conn.cursor()
    
    # 创建表
    c.execute('''CREATE TABLE IF NOT EXISTS transactions
                 (id INTEGER PRIMARY KEY, date TEXT, type TEXT, amount REAL, description TEXT)''')
    conn.commit()
    

  3. 定义函数

    def add_transaction(date, trans_type, amount, description):
        c.execute("INSERT INTO transactions (date, type, amount, description) VALUES (?, ?, ?, ?)",
                  (date, trans_type, amount, description))
        conn.commit()
    
    def generate_report(period):
        if period == 'monthly':
            c.execute("SELECT strftime('%Y-%m', date) as month, type, SUM(amount) FROM transactions GROUP BY month, type")
        elif period == 'yearly':
            c.execute("SELECT strftime('%Y', date) as year, type, SUM(amount) FROM transactions GROUP BY year, type")
    
        report = c.fetchall()
        for row in report:
            print(row)
    

  4. 主程序

    def main():
        while True:
            print("\n个人记账应用")
            print("1. 记录收入")
            print("2. 记录支出")
            print("3. 生成月度报告")
            print("4. 生成年度报告")
            print("5. 退出")
    
            choice = input("请选择操作: ")
    
            if choice == '1':
                date = input("请输入日期 (YYYY-MM-DD): ")
                amount = float(input("请输入金额: "))
                description = input("请输入描述: ")
                add_transaction(date, '收入', amount, description)
                print("收入记录成功!")
    
            elif choice == '2':
                date = input("请输入日期 (YYYY-MM-DD): ")
                amount = float(input("请输入金额: "))
                description = input("请输入描述: ")
                add_transaction(date, '支出', amount, description)
                print("支出记录成功!")
    
            elif choice == '3':
                print("月度报告:")
                generate_report('monthly')
    
            elif choice == '4':
                print("年度报告:")
                generate_report('yearly')
    
            elif choice == '5':
                break
    
            else:
                print("无效的选择,请重新选择。")
    
    if __name__ == "__main__":
        main()
        conn.close()
    

完整代码

import psutil
import time

def get_current_processes():
    # 获取当前所有进程的pid和名称
    return {p.pid: p.name() for p in psutil.process_iter(['pid', 'name'])}

def print_new_processes(old_processes):
    # 获取新的进程
    current_processes = get_current_processes()
    new_processes = {pid: name for pid, name in current_processes.items() if pid not in old_processes}

    # 打印新进程的pid和名称
    for pid, name in new_processes.items():
        print(f"New process detected: PID={pid}, Name={name}")

    # 打印新进程的数量
    print(f"Number of new processes: {len(new_processes)}")

    return current_processes

if __name__ == "__main__":
    processes = get_current_processes()

    while True:
        processes = print_new_processes(processes)
        time.sleep(1)  # 每秒检查一次

运行结果

运行该脚本后,程序将提示你输入日期、金额和描述来记录收入或支出。选择生成月度或年度报告来查看汇总的收入和支出。例如:

New process detected: PID=1234, Name=python.exe
Number of new processes: 1

结论

这个脚本使用psutil库来监控系统中的新进程。通过每秒检查一次系统中的进程,并打印出新创建的进程的PID和名称,我们可以更好地理解系统的行为和进行性能分析。希望这个脚本对你有所帮助!


其他文章推荐

  1. 使用 Python和moviepy库 将MP4视频 文件转换为GIF动画-CSDN博客
  2. 在 Python 中以特定格式打印所有 ASCII 字符-CSDN博客
  3. Python 创建一个简单的在线聊天系统-CSDN博客
  4. 使用 Python 的 math 库进行基本的数学计算-CSDN博客
  5. PyQt5 创建个人记账应用-CSDN博客

总结

这个简单的Python脚本可以帮助我们监控新进程的创建,对于理解系统行为和进行性能分析非常有用。希望你喜欢这篇博客,如果有任何问题或建议,欢迎留言讨论。下次再见!

 

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIY若依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值