写了数据库表信息的.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文件连接起来呢?拜托
把数据库代码的py当成一个包,进行import
那么如何做呢???
首先打开pycharm,点击左上角文件下的设置。
然后点击编辑器下的文件和代码模板中的Python Script。
在红色圈圈的部分把你需要添加的前缀代码复制粘贴过去,一般比较常用的就是下面给出的这几个。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : ${DATE} ${TIME}
# @Author : GXl
# @File : ${NAME}.py
# @Software: win10 Tensorflow1.13.1 python3.6.3
@Time系统会自动填写当前对应的日期和时间。
@File系统会自动填写当前对应的文件的名字。
对应的==@Software==需要自己查询并填写。