mysql查询网址_bootstrap+flask+mysql实现网站查询
之前那篇文章是flask+redis的,如果用flask+mysql怎么實現(xiàn)呢?
創(chuàng)建數(shù)據(jù)庫:CREATE?DATABASE?`web12306`??DEFAULT?CHARACTER?SET?utf8;
創(chuàng)建表:CREATE?TABLE?`web12306`?(
`user_email`?varchar(100)?NOT?NULL?DEFAULT?'',
`user_pass`?varchar(100)?NOT?NULL?DEFAULT?'',
`user_name`?varchar(100)?NOT?NULL?DEFAULT?'',
`user_id`?varchar(100)?NOT?NULL?DEFAULT?'',
`user_nic`?varchar(100)?NOT?NULL?DEFAULT?'',
`user_phone`?varchar(100)?NOT?NULL?DEFAULT?''
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8;
導(dǎo)入數(shù)據(jù),數(shù)據(jù)文件還是result.txt,同樣的格式。mysql?-uroot?-pmysql@123?-e?'load?data?local?infile?"result.txt"?into?table?web12306.web12306?fields?terminated?by?"---"?lines?terminated?by?"\n";'
查看數(shù)據(jù)導(dǎo)入情況:
數(shù)據(jù)有了,剩下還是上python代碼了,使用mysql時,需要用到sqlalchemy#?-*-?coding:?utf-8?-*-
from?flask?import?Flask,?request,?render_template,?jsonify,?json
from?flask.ext.sqlalchemy?import?SQLAlchemy
app?=?Flask(__name__)
db?=?SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI']?=?'mysql://root:mysql@123@localhost/web12306'
class?session(db.Model):
__tablename__?=?'web12306'
user_id?=?db.Column(db.String(100),?primary_key?=?True)
user_email?=?db.Column(db.String(100))
user_pass?=?db.Column(db.String(100))
user_nic?=?db.Column(db.String(100))
user_phone?=?db.Column(db.String(100))
user_name?=?db.Column(db.String(100))
@app.route('/scan/',?methods=['GET'])
def?scan(user_id):
result?=?session.query.filter_by(user_id=user_id).first()
if?result?is?None:
json_result={'user_id':None}
return?json.dumps(json_result,ensure_ascii=False)
else:
json_result?=?{'user_id':?result.user_id,?'user_email':?result.user_email,?'user_pass':?result.user_pass,?'user_nic':?result.user_nic,?'user_phone':?result.user_phone,?'user_name':?result.user_name}
return?json.dumps(json_result,ensure_ascii=False)
@app.route('/')
def?index():
return?render_template('index.html')
if?__name__?==?'__main__':
app.run(host='0.0.0.0',?port?=?8080,?debug=True)
前端代碼請看上一篇文字。
總結(jié)
以上是生活随笔為你收集整理的mysql查询网址_bootstrap+flask+mysql实现网站查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡到期换卡会降低额度吗?有这几种可能
- 下一篇: 信用卡逾期多久抓人?原来不是吓唬人的