Three flask+ MySQL database operation

Source: Internet
Author: User

One: __init__.py effect:

The purpose of the __init__.py file is to change the folder into a Python module , with a__init__.py file in each of the modules in the Python package. Normally the __init__.py file is empty, but we can add additional functionality to it. when we imported a package, we actually imported its __init__.py file . This allows us to bulk import the modules we need in the __init__.py file, instead of needing one import.

__init__.py:

 from Import Flask, url_for, request, redirect, Render_template  from Import  = Flask (__name__) app.config.from_object ('config')       = SQLAlchemy (APP)                  fromimport models,views

models.py

__TABLENAME__: Table name.

In __repr__:python, this _repr_ function, which corresponds to the Repr (object) function, returns a printable string that can be used to represent an object. You can print out objects

 fromAppImportDb#DB is an associated SQLAlchemy instance that is generated in app/__init__.pyclassUser (db. Model):__tablename__='Users'ID= db. Column (db. Integer, primary_key=True) Username= db. Column (db. String (unique=),True) Email= db. Column (db. String (unique=),True) Password= db. Column (db. String (+), nullable=False)def __repr__(self):return '<user%r>'%Self.usernameclassAdmin (db. Model):__tablename__='Admins'ID= db. Column (db. Integer, primary_key=True) Username= db. Column (db. String (unique=),True) Email= db. Column (db. String (unique=),True) Password= db. Column (db. String (+), nullable=False)def __repr__(self):return '<user%r>'% Self.username

create_db.py

Create_all: This function will find the tables, then connect the DB, then create the table.
 from Import Dbdb.create_all ()

Execute Python create_db.py with the command line; or directly in Pycharm, right-create_db.py will create the table.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////

Three flask+ MySQL database operation

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.