Code for assisted development using SQLAlchemy in flask

Source: Internet
Author: User
Installation method

1), Apt-ge installation

The code is as follows:


sudo apt-get install Flask-sqlalchemy

2), download the installation package for installation

The code is as follows:


# can be used directly in the PY after installation
Import Flask
From Flask.ext.sqlalchemy import SQLAlchemy

App = Flask. Flask (__name__)
#-Settings is configured with SQLAlchemy database address
# SQLite ex: "Sqlite:///dbname.db"
App.config.from_object ("Settings")
db = SQLAlchemy (APP)
Db.init_app (APP)
#-Create_all () will create all inherited db when called. Model Template
# Model ex: see Class Admininfo
Db.create_all ()

Class Admininfo (db. Model):
ID = db. Column (db. Integer, Primary_key = True)
Name = db. Column (db. String (16))
Password = db. Column (db. String (32))
Kidname = db. Column (db. String (16))
Diy_show = db. Column (db. Text)

def __init__ (self, name, password, kidname, diy_show):
Self.name = Name
Self.password = password
Self.kidname = Kidname
Self.diy_show = Diy_show

def __repr__ (self):
Return " "% (self.name, ' * ' *len (Self.password))

This means that you can use SQLAlchemy in the render template.

The code is as follows:


# operate on the Admininfo
ai = Admininfo ("gaoyiping", "gaoyiping", U "I call Gao Yiping", u "Hello everyone, my name is Gao Yiping, what's your name?" Let's make a friend. ")
# so we've already instantiated a SQL Data
# Insert the DB
Db.session.add (AI)
# for DB commit
Db.session.commit ()

# If you make a query
AdminInfo.query.all ()
# >>> [ ,]
AdminInfo.query.get (1) # Query the first record you just inserted
# >>>
AdminInfo.query.filter_by (name = "Gaoyiping")
# >>>

  • 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.