Bootstrap+flask+mysql Writing Query page

Source: Internet
Author: User

Before that article is Flask+redis, if using flask+mysql how to achieve it?

To create a database:

CREATE DATABASE ' web12306 ' DEFAULT CHARACTER SET UTF8;

To create a table:

CREATE TABLE ' web12306 ' (' user_email ' varchar () NOT NULL default ' ', ' user_pass ' varchar (+) ' NOT null default ' ', ' user_name ' varchar (+) NOT null default ' ', ' user_id ' varchar (+) NOT null default ' ', ' user_nic ' varchar (+) not NU LL default ' ', ' user_phone ' varchar (+) not NULL default ') Engine=innodb default Charset=utf8;

Import data, data files or result.txt, the same format.

mysql-uroot [email protected]-E ' load data local infile "result.txt" into table web12306.web12306 fields terminated by " ---"lines terminated by" \ n "; '


To view the data import situation:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/10/wKiom1T-4QnhxW_4AAUr9mgwFGw547.jpg "title=" my.png "alt=" Wkiom1t-4qnhxw_4aaur9mgwfgw547.jpg "/>

The data is there, the remaining Python code, using MySQL, you need to use the SQLAlchemy

# -*- coding: utf-8 -*-from flask import flask, request, render_ template, jsonify, jsonfrom flask.ext.sqlalchemy import sqlalchemyapp =  Flask (__name__) db = sqlalchemy (APP) app.config[' Sqlalchemy_database_uri '] =  ' mysql://root:[ Email protected] @localhost/web12306 ' class session (db. Model):  __tablename__ =  ' web12306 '   user_id = db. Column (db. String (+),  primary_key = true)   user_email = db. Column (db. String (&NBSP;&NBSP;USER_PASS&NBSP;=&NBSP;DB). Column (db. String (&NBSP;&NBSP;USER_NIC&NBSP;=&NBSP;DB). Column (db. String (&NBSP;&NBSP;USER_PHONE&NBSP;=&NBSP;DB). Column (db. String (&NBSP;&NBSP;USER_NAME&NBSP;=&NBSP;DB). Column (db. String @app. Route ('/scan/<user_id> ',  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)

Front-end code please look at a text.

This article from "Lao Xu's Private Food" blog, declined to reprint!

Bootstrap+flask+mysql Writing Query page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.