forked from wintests/flaskDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
from flask import Flask | ||
import os, sys | ||
from config.setting import SERVER_PORT | ||
from api.user import app | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello World!' | ||
# 项目根路径 | ||
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
sys.path.insert(0, BASE_PATH) # 将项目根路径临时加入环境变量,程序退出后失效 | ||
|
||
if __name__ == '__main__': | ||
app.run() | ||
# host为主机ip地址,port指定访问端口号,debug=True设置调试模式打开 | ||
app.run(host="0.0.0.0", port=SERVER_PORT, debug=True) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pymysql | ||
from config.setting import MYSQL_HOST,MYSQL_PORT,MYSQL_USER,MYSQL_PASSWD,MYSQL_DB | ||
|
||
class MysqlDb(): | ||
|
||
def __init__(self, host, port, user, passwd, db): | ||
# 建立数据库连接 | ||
self.conn = pymysql.connect( | ||
host=host, | ||
port=port, | ||
user=user, | ||
passwd=passwd, | ||
db=db | ||
) | ||
# 通过 cursor() 创建游标对象,并让查询结果以字典格式输出 | ||
self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor) | ||
|
||
def __del__(self): # 对象资源被释放时触发,在对象即将被删除时的最后操作 | ||
# 关闭游标 | ||
self.cur.close() | ||
# 关闭数据库连接 | ||
self.conn.close() | ||
|
||
def select_db(self, sql): | ||
"""查询""" | ||
# 使用 execute() 执行sql | ||
self.cur.execute(sql) | ||
# 使用 fetchall() 获取查询结果 | ||
data = self.cur.fetchall() | ||
return data | ||
|
||
def execute_db(self, sql): | ||
"""更新/新增/删除""" | ||
try: | ||
# 使用 execute() 执行sql | ||
self.cur.execute(sql) | ||
# 提交事务 | ||
self.conn.commit() | ||
except Exception as e: | ||
print("操作出现错误:{}".format(e)) | ||
# 回滚所有更改 | ||
self.conn.rollback() | ||
|
||
db = MysqlDb(MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWD, MYSQL_DB) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# 服务端口配置 | ||
SERVER_PORT = 9999 | ||
|
||
# MySQL配置 | ||
MYSQL_HOST = "192.168.89.128" | ||
MYSQL_PORT = 3306 | ||
MYSQL_USER = "root" | ||
MYSQL_PASSWD = "123456" | ||
MYSQL_DB = "flask_demo" |