如何利用MySQL和Python開發一個簡單的線上投資管理系統
引言:
隨著網路的不斷發展,越來越多的人開始將資產投資於各種投資項目。對於投資者來說,一個簡單易用的線上投資管理系統可以幫助他們更好地管理和追蹤自己的投資。本文將介紹如何利用MySQL和Python開發一個簡單的線上投資管理系統,並提供具體的程式碼範例。
一、系統需求分析
在開始開發之前,我們需要先明確所要開發的系統的功能需求。一個簡單的線上投資管理系統應該包括以下功能:
二、開發環境準備
在開始開發之前,我們需要準備好開發環境。本文將使用MySQL資料庫和Python程式語言來建構開發環境。
三、資料庫設計
接下來,我們需要設計資料庫表格結構來儲存系統的資料。本文將設計三個資料表:使用者表、專案表和投資記錄表。
四、系統開發
在開發之前,我們需要導入所需的函式庫和模組,如pymysql、flask等。
定義資料庫連線和遊標物件。
import pymysql connection = pymysql.connect(host='localhost', user='root', password='password', database='investments') cursor = connection.cursor()
建立使用者註冊和登入功能。
# 用户注册 @app.route('/register', methods=['POST']) def register(): username = request.form['username'] password = request.form['password'] cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password)) connection.commit() return redirect(url_for('login')) # 用户登录 @app.route('/login', methods=['POST']) def login(): username = request.form['username'] password = request.form['password'] cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s", (username, password)) if cursor.fetchone() is not None: session['username'] = username return redirect(url_for('home')) else: return redirect(url_for('login'))
建立專案管理功能。
# 创建项目 @app.route('/create-project', methods=['POST']) def create_project(): name = request.form['name'] start_date = request.form['start_date'] investment_amount = request.form['investment_amount'] cursor.execute("INSERT INTO projects (name, start_date, investment_amount) VALUES (%s, %s, %s)", (name, start_date, investment_amount)) connection.commit() return redirect(url_for('projects')) # 修改项目 @app.route('/edit-project/<int:project_id>', methods=['POST']) def edit_project(project_id): name = request.form['name'] start_date = request.form['start_date'] investment_amount = request.form['investment_amount'] cursor.execute("UPDATE projects SET name = %s, start_date = %s, investment_amount = %s WHERE id = %s", (name, start_date, investment_amount, project_id)) connection.commit() return redirect(url_for('projects')) # 删除项目 @app.route('/delete-project/<int:project_id>', methods=['POST']) def delete_project(project_id): cursor.execute("DELETE FROM projects WHERE id = %s", (project_id,)) connection.commit() return redirect(url_for('projects'))
建立投資記錄管理功能。
# 创建投资记录 @app.route('/create-investment', methods=['POST']) def create_investment(): date = request.form['date'] amount = request.form['amount'] project_id = request.form['project_id'] cursor.execute("INSERT INTO investments (date, amount, project_id, user_id) VALUES (%s, %s, %s, %s)", (date, amount, project_id, session['username'])) connection.commit() return redirect(url_for('investments')) # 修改投资记录 @app.route('/edit-investment/<int:investment_id>', methods=['POST']) def edit_investment(investment_id): date = request.form['date'] amount = request.form['amount'] project_id = request.form['project_id'] cursor.execute("UPDATE investments SET date = %s, amount = %s, project_id = %s WHERE id = %s", (date, amount, project_id, investment_id)) connection.commit() return redirect(url_for('investments')) # 删除投资记录 @app.route('/delete-investment/<int:investment_id>', methods=['POST']) def delete_investment(investment_id): cursor.execute("DELETE FROM investments WHERE id = %s", (investment_id,)) connection.commit() return redirect(url_for('investments'))
建立投資統計和報表功能。
# 投资统计 @app.route('/investment-stats') def investment_stats(): cursor.execute("SELECT SUM(amount) FROM investments WHERE user_id = %s", (session['username'],)) total_investment = cursor.fetchone()[0] return render_template('investment_stats.html', total_investment=total_investment) # 生成报表 @app.route('/generate-report') def generate_report(): cursor.execute("SELECT * FROM investments WHERE user_id = %s", (session['username'],)) investments = cursor.fetchall() return render_template('report.html', investments=investments)
運行系統。
if __name__ == '__main__': app.secret_key = 'secret_key' app.run(debug=True)
五、總結
本文簡要介紹如何利用MySQL和Python開發一個簡單的線上投資管理系統,並提供了相關的程式碼範例。這個系統可以幫助投資人更好地管理和追蹤自己的投資,包括專案管理、投資記錄管理、投資統計和報表等功能。開發者可以根據自己的需求進一步完善系統,並運用到實際的投資管理中。
以上是如何利用MySQL和Python開發一個簡單的線上投資管理系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!