丢了兔子的尾巴 2023-06-13 08:53 采纳率: 84%
浏览 12
已结题

pycharm中写了数据库信息的.py文件和写了网页代码的.py文件该怎么连接

写了数据库表信息的.py文件代码如下

import pymysqldb = pymysql.connect(        host='localhost',        port=3306,        user='root',        password='123456',        charset='utf8mb4',        database='A1'    )cursor = db.cursor() ad = 'create table if not exists goods(奖品编号 varchar(20),名字 varchar(20),网上链接 varchar(20),价值 varchar(10))'cursor.execute(ad)bd = 'create table if not exists draw(编号 varchar(20),抽奖名称 varchar(20),奖项编号 varchar(20))'cursor.execute(bd)cd = 'create table  if not exists award(编号 varchar(20),奖项名称 varchar(20),奖品编号 varchar(20),抽取人数 int(10))'cursor.execute(cd)sql = 'create table  if not exists user(username varchar(20),password char(20),realname varchar(20))'cursor.execute(sql)sql = "insert into user(username,password,realname) values({},{},'{}');"真实姓名_s = ['张三','里越','里斯','李华','徐华','胡榫']for i in range(len(真实姓名_s)):    cursor.execute(sql.format(i,'666',真实姓名_s[i]))db.commit()

写了网页代码的.py文件

from flask import Flask,render_template,requestimport modelapp = Flask(__name__)@app.route('/')def index():    return render_template("login.html")@app.route('/home')def home():    return render_template("home.html")@app.route('/login')def login():    return render_template("home.html")@app.route('/checklogin', methods=['POST'])def checklogin():    n = request.form.get("name")    p = request.form.get("pwd")    sql = 'select pwd from user where unname="{}"'.format(n)    pwd = model.executeSql(sql)    print(pwd)    if len(pwd) == 0:        return '<script> alert("用户不存在!");window.open("/login");</script>'    if p == pwd[0][0]:        return render_template("home.html")    else:        return '<script> alert("用户名或密码错误!");window.open("/login"); </script>'@app.route('/register')def register():    return render_template("register.html")@app.route('/checkregister', methods=['POST'])def checkregister():    n = request.form.get("name")    p = request.form.get("pwd")    sql = 'select pwd from user where unname="{}"'.format(n)    pwd = model.executeSql(sql)    print(pwd)    if len(pwd) == 0:        return '<script> alert("用户不存在!");window.open("/register");</script>'    if p == pwd[0][0]:        return render_template("home.html")    else:        return '<script> alert("用户名或密码错误!");window.open("/register"); </script>'        return render_template("login.html")@app.route('/addAward')def addAward():    return render_template("addAward.html")@app.route('/addDraw')def addDraw():    return render_template("addDraw.html")@app.route('/addGoods')def addGoods():    return render_template("addGoods.html")@app.route('/draw')def draw():    return render_template("draw.html")@app.route('/draw_end')def draw_end():    return render_template("draw_end.html")app.run()

我该怎么做能让写了数据库表代码的.py文件和网页的代码的.py文件连接起来呢?拜托

  • 写回答

2条回答 默认 最新

  • 一包辣条九毛九 2023-06-13 09:50
    关注

    把数据库代码的py当成一个包,进行import

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 创建了问题 6月13日