迁移数据库为postgresql

This commit is contained in:
2026-07-17 18:43:24 +08:00
parent 690b4e2ed3
commit 7f0f9df727
10 changed files with 290 additions and 33 deletions

View File

@@ -1,9 +1,24 @@
from flask import Blueprint, render_template, request, url_for, flash, redirect
from flask import Blueprint, render_template, request, url_for, flash, redirect, session
import os
import sqlite3
import psycopg2, json, hashlib
study_bp = Blueprint('study', __name__)
# pgdb_conn
# 读取json文件
with open('/var/open-ww3-project-ww3-tw/config/study/db_config.json', 'r') as file:
data = json.load(file)
print(data)
# 设定数据库连接配置
conn_params = {
"dbname" : data['dbname'],
"user" : data['user'],
"password": data['password'],
"host": data['host'],
"port": data['port']
}
def get_db_conn():
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/database.db')
conn.row_factory = sqlite3.Row
@@ -93,4 +108,99 @@ def delete_posts_id(posts_id):
conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,))
conn.commit()
conn.close()
return render_template('study/delete.html', posts=posts)
return render_template('study/delete.html', posts=posts)
@study_bp.route('/bbs/', methods=['POST' , 'GET'])
def pgdb():
# 连接数据库
try:
conn = psycopg2.connect(**conn_params)
print("连接没问题!!!")
cur = conn.cursor()
# cur.execute("select * from study_pg;")
# db_cur = cur.fetchone()
cur.close()
conn.close()
# return f"连接没问题!!!<br>数据库内容: {db_cur}"
return render_template("study/bbs.html")
# , db_cur = db_cur)
except Exception as e:
print(f"出现了一点状况: {e}")
cur = conn.cursor()
cur.close()
conn.close()
return f"出现了一点状况: {e}"
@study_bp.route('/bbs/register/', methods=['POST' , 'GET'])
def pgdb_register():
if request.method == "GET":
return render_template('study/register.html')
if request.method == "POST":
username = request.form.get('username')
passwd = request.form.get('passwd')
age = request.form.get('age')
hash_obj = hashlib.sha256()
hash_obj.update(passwd.encode())
passwd_hash = hash_obj.hexdigest()
nbsp = "&nbsp;" *4
# 数据库操作
conn = psycopg2.connect(**conn_params)
cur = conn.cursor()
sql_check = f"select username from user_temp where username = '{username}';"
cur.execute(sql_check)
sql_name = cur.fetchone()
# cur.close()
# conn.close()
if sql_name == None:
sql_insert = f"insert into user_temp (username, passwd, age) values ('{username}', '{passwd_hash}', '{age}');"
cur.execute(sql_insert)
conn.commit()
conn.close()
return "名字没问题允许注册" \
"用户名: " f"{nbsp}" f"{username}" "<br>" \
"密码: " f"{nbsp}" f"{passwd}" "<br>" \
"年龄: " f"{nbsp}" "**" "<br>" "<br>" \
"<b>密码为哈希存储,只会展示一次<br>" \
"如果忘记密码了,建议重新注册</b>" \
# "<br>" "<br>" f"{sql_name}"
else:
conn.close()
return "名字大错特错!!!不允许注册<br><a href='./'>返回注册页面</a>"
# sql_insert = f"insert into user_temp (username, passwd, age) values ('{username}', '{passwd_hash}', '{age}');"
# cur.execute(sql_insert)
# conn.commit()
# cur.close()
# conn.close()
@study_bp.route('/bbs/login/', methods=['POST', 'GET'])
def pgdb_login():
if request.method == "GET":
return render_template('study/login.html')
if request.method == "POST":
username = request.form.get('username', '')
passwd = request.form.get('passwd', '')
if not username or not passwd:
return "用户名或密码不能空<br><a href='./'>返回重登</a>"
hash_passwd = hashlib.sha256(passwd.encode()).hexdigest()
conn = psycopg2.connect(**conn_params)
cur = conn.cursor()
cur.execute(
"SELECT id, passwd FROM user_temp WHERE username = %s",
(username,)
)
row = cur.fetchone()
cur.close()
conn.close()
if row and row[1] == hash_passwd:
session['user_id'] = row[0]
return "登录成功<br>"
else:
return "登录失败了.....<br><a href='./'>点击重新登录</a>"