使用pycharm和mysql制作个人事务管理系统,其中包括主要功能: 1. 通讯录管理:对个人好友信息的添加、更新、删除、查询和报表输出; 2. 日志管理:对个人日志的添加、更新、删除和查询; 3. 个人财务管理:对个人的收入和支出信息进行维护。
时间: 2023-12-20 19:03:39 浏览: 83
个人事务管理系统
4星 · 用户满意度95%
好的,那我们可以分几个步骤来实现这个系统。
1. 安装MySQL数据库和PyCharm
首先需要安装MySQL数据库和PyCharm,可以从官网下载安装包,根据提示安装即可。
2. 创建数据库和表
在MySQL中创建一个名为“personal_management”的数据库,然后创建三个表,分别为“address_book”、“log”和“finance”。可以使用以下代码创建表:
```sql
CREATE TABLE `address_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`content` varchar(500) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `finance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(10) DEFAULT NULL,
`amount` decimal(10,2) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
3. 编写Python代码
在PyCharm中创建一个新项目,然后创建三个Python文件,分别为“address_book.py”、“log.py”和“finance.py”。在这些文件中,可以使用Python的MySQL库来连接数据库,并编写相应的函数来实现添加、更新、删除、查询和报表输出等功能。
例如,以下是一个“address_book.py”文件的示例代码:
```python
import mysql.connector
class AddressBook:
def __init__(self):
self.conn = mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='personal_management'
)
def add_contact(self, name, phone, email):
cursor = self.conn.cursor()
sql = "INSERT INTO address_book (name, phone, email) VALUES (%s, %s, %s)"
val = (name, phone, email)
cursor.execute(sql, val)
self.conn.commit()
cursor.close()
def update_contact(self, id, name=None, phone=None, email=None):
cursor = self.conn.cursor()
sql = "UPDATE address_book SET "
if name:
sql += "name=%s, "
if phone:
sql += "phone=%s, "
if email:
sql += "email=%s, "
sql = sql[:-2] + " WHERE id=%s"
val = (name, phone, email, id)
cursor.execute(sql, val)
self.conn.commit()
cursor.close()
def delete_contact(self, id):
cursor = self.conn.cursor()
sql = "DELETE FROM address_book WHERE id=%s"
val = (id,)
cursor.execute(sql, val)
self.conn.commit()
cursor.close()
def search_contact(self, name=None, phone=None, email=None):
cursor = self.conn.cursor()
sql = "SELECT * FROM address_book WHERE 1=1 "
if name:
sql += "AND name LIKE '%" + name + "%' "
if phone:
sql += "AND phone LIKE '%" + phone + "%' "
if email:
sql += "AND email LIKE '%" + email + "%' "
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
def generate_report(self):
cursor = self.conn.cursor()
sql = "SELECT name, phone, email FROM address_book"
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
report = ""
for row in result:
report += "Name: " + row[0] + ", Phone: " + row[1] + ", Email: " + row[2] + "\n"
return report
```
这个示例代码实现了添加、更新、删除、查询和报表输出等功能,使用了MySQL的INSERT、UPDATE、DELETE和SELECT语句。其他文件的代码也可以类似地编写。
4. 编写用户界面
最后,可以使用Python的tkinter库来编写用户界面,让用户可以方便地使用这个系统。可以创建一个“main.py”文件,然后在其中创建一个主窗口,并添加按钮和文本框等控件来实现用户交互。例如:
```python
import tkinter as tk
from address_book import AddressBook
from log import Log
from finance import Finance
class Main:
def __init__(self):
self.address_book = AddressBook()
self.log = Log()
self.finance = Finance()
self.window = tk.Tk()
self.window.title("Personal Management System")
self.tab_control = tk.Notebook(self.window)
self.address_book_tab = tk.Frame(self.tab_control)
self.log_tab = tk.Frame(self.tab_control)
self.finance_tab = tk.Frame(self.tab_control)
self.tab_control.add(self.address_book_tab, text="Address Book")
self.tab_control.add(self.log_tab, text="Log")
self.tab_control.add(self.finance_tab, text="Finance")
self.create_address_book_tab()
self.create_log_tab()
self.create_finance_tab()
self.tab_control.pack(expand=1, fill="both")
self.window.mainloop()
def create_address_book_tab(self):
lbl_name = tk.Label(self.address_book_tab, text="Name")
lbl_phone = tk.Label(self.address_book_tab, text="Phone")
lbl_email = tk.Label(self.address_book_tab, text="Email")
lbl_name.grid(row=0, column=0)
lbl_phone.grid(row=1, column=0)
lbl_email.grid(row=2, column=0)
self.ent_name = tk.Entry(self.address_book_tab)
self.ent_phone = tk.Entry(self.address_book_tab)
self.ent_email = tk.Entry(self.address_book_tab)
self.ent_name.grid(row=0, column=1)
self.ent_phone.grid(row=1, column=1)
self.ent_email.grid(row=2, column=1)
btn_add = tk.Button(self.address_book_tab, text="Add Contact", command=self.add_contact)
btn_update = tk.Button(self.address_book_tab, text="Update Contact", command=self.update_contact)
btn_delete = tk.Button(self.address_book_tab, text="Delete Contact", command=self.delete_contact)
btn_search = tk.Button(self.address_book_tab, text="Search Contact", command=self.search_contact)
btn_report = tk.Button(self.address_book_tab, text="Generate Report", command=self.generate_report)
btn_add.grid(row=3, column=0)
btn_update.grid(row=3, column=1)
btn_delete.grid(row=4, column=0)
btn_search.grid(row=4, column=1)
btn_report.grid(row=5, column=0, columnspan=2)
self.txt_result = tk.Text(self.address_book_tab, height=10, width=50)
self.txt_result.grid(row=6, column=0, columnspan=2)
def create_log_tab(self):
pass # similar to create_address_book_tab
def create_finance_tab(self):
pass # similar to create_address_book_tab
def add_contact(self):
name = self.ent_name.get()
phone = self.ent_phone.get()
email = self.ent_email.get()
self.address_book.add_contact(name, phone, email)
self.txt_result.insert("end", "Contact added successfully.\n")
def update_contact(self):
id = self.ent_id.get()
name = self.ent_name.get()
phone = self.ent_phone.get()
email = self.ent_email.get()
self.address_book.update_contact(id, name, phone, email)
self.txt_result.insert("end", "Contact updated successfully.\n")
def delete_contact(self):
id = self.ent_id.get()
self.address_book.delete_contact(id)
self.txt_result.insert("end", "Contact deleted successfully.\n")
def search_contact(self):
name = self.ent_name.get()
phone = self.ent_phone.get()
email = self.ent_email.get()
result = self.address_book.search_contact(name, phone, email)
self.txt_result.delete(1.0, "end")
for row in result:
self.txt_result.insert("end", "ID: " + str(row[0]) + ", Name: " + row[1] + ", Phone: " + row[2] + ", Email: " + row[3] + "\n")
def generate_report(self):
report = self.address_book.generate_report()
self.txt_result.delete(1.0, "end")
self.txt_result.insert("end", report)
Main()
```
这个代码创建了一个包含三个选项卡的主窗口,每个选项卡对应一个功能模块。在每个选项卡中,通过创建标签、输入框、按钮和文本框等控件来实现用户交互。例如,在“Address Book”选项卡中,有一个“Add Contact”按钮,当用户点击该按钮时,程序会调用add_contact函数来添加一个新的联系人,并在文本框中显示操作结果。
这里只是一个简单的示例,实际上可以根据需求来添加更多的功能和控件。
阅读全文