使用pycharm和mysql写一个个人事务管理系统;包含 主要功能: 1. 通讯录管理:对个人好友信息的添加、更新、删除、查询和报表输出; 2. 日志管理:对个人日志的添加、更新、删除和查询; 3. 个人财务管理:对个人的收入和支出信息进行维护。
时间: 2024-02-05 19:05:07 浏览: 88
这是一个比较复杂的项目,需要涉及到多个方面的知识,包括Python编程、MySQL数据库、Web开发等等。如果您需要一个详细的指导,可能需要更多的时间和精力,以下是一个简单的示例供参考。
1. 环境搭建
首先需要安装PyCharm和MySQL,并创建一个名为"personal_management"的数据库。
2. 数据库设计
根据需求,我们需要创建三个表:friends、logs和finance。
friends表包含字段:id、name、phone、email、address;
logs表包含字段:id、title、content、create_time、update_time;
finance表包含字段:id、type、amount、create_time。
可以使用以下SQL语句在MySQL中创建这三个表:
```
CREATE TABLE `friends` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`phone` varchar(20) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `finance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(50) NOT NULL,
`amount` decimal(10,2) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
3. Python编程
在PyCharm中创建一个新的Python项目,然后创建一个名为"main.py"的主程序文件。使用以下代码连接到MySQL数据库:
```
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="personal_management"
)
mycursor = mydb.cursor()
```
然后可以编写各个功能的代码,以通讯录管理为例:
添加好友:
```
def add_friend(name, phone, email, address):
sql = "INSERT INTO friends (name, phone, email, address) VALUES (%s, %s, %s, %s)"
val = (name, phone, email, address)
mycursor.execute(sql, val)
mydb.commit()
```
更新好友:
```
def update_friend(id, name, phone, email, address):
sql = "UPDATE friends SET name = %s, phone = %s, email = %s, address = %s WHERE id = %s"
val = (name, phone, email, address, id)
mycursor.execute(sql, val)
mydb.commit()
```
删除好友:
```
def delete_friend(id):
sql = "DELETE FROM friends WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
```
查询好友:
```
def get_friends():
sql = "SELECT * FROM friends"
mycursor.execute(sql)
return mycursor.fetchall()
```
报表输出:
```
def print_friends():
friends = get_friends()
for friend in friends:
print("ID: " + str(friend[0]))
print("Name: " + friend[1])
print("Phone: " + friend[2])
print("Email: " + friend[3])
print("Address: " + friend[4])
print("=========================")
```
4. Web开发
如果需要将这个个人事务管理系统做成一个Web应用程序,可以使用Flask框架。首先需要安装Flask:
```
pip install flask
```
然后在PyCharm中创建一个名为"app.py"的文件,编写以下代码:
```
from flask import Flask, render_template, request, redirect
import mysql.connector
app = Flask(__name__)
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="personal_management"
)
mycursor = mydb.cursor()
@app.route('/')
def index():
friends = get_friends()
return render_template('index.html', friends=friends)
@app.route('/add_friend', methods=['POST'])
def add_friend_handler():
name = request.form['name']
phone = request.form['phone']
email = request.form['email']
address = request.form['address']
add_friend(name, phone, email, address)
return redirect('/')
@app.route('/update_friend', methods=['POST'])
def update_friend_handler():
id = request.form['id']
name = request.form['name']
phone = request.form['phone']
email = request.form['email']
address = request.form['address']
update_friend(id, name, phone, email, address)
return redirect('/')
@app.route('/delete_friend', methods=['POST'])
def delete_friend_handler():
id = request.form['id']
delete_friend(id)
return redirect('/')
def get_friends():
sql = "SELECT * FROM friends"
mycursor.execute(sql)
return mycursor.fetchall()
def add_friend(name, phone, email, address):
sql = "INSERT INTO friends (name, phone, email, address) VALUES (%s, %s, %s, %s)"
val = (name, phone, email, address)
mycursor.execute(sql, val)
mydb.commit()
def update_friend(id, name, phone, email, address):
sql = "UPDATE friends SET name = %s, phone = %s, email = %s, address = %s WHERE id = %s"
val = (name, phone, email, address, id)
mycursor.execute(sql, val)
mydb.commit()
def delete_friend(id):
sql = "DELETE FROM friends WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
if __name__ == '__main__':
app.run(debug=True)
```
这个应用程序包含了一个主页,可以显示所有的好友信息,并提供添加、更新和删除好友的功能。可以使用以下命令启动应用程序:
```
python app.py
```
然后在浏览器中访问"http://localhost:5000/"即可查看效果。
以上仅为一个简单的示例,实际上需要更多的代码和细节处理,包括输入验证、错误处理、数据统计等等。如果您需要更详细的指导,请告诉我。
阅读全文