Example of using Core mode of SQLAlchemy

Source: Internet
Author: User
Tags sqlite

SQLAlchemy: What is the experience of operating data in the Core mode?

Answer: Cool!

This article is based on: Win + python 3.4 + sqlalchemy 1.0.13

The basic steps are as follows:

1. Binding the Database
 from Import  = create_engine ('sqlite:///:memory:', echo=true)

2. Connect to the database
conn = Engine.connect ()

3. Meta-data
Import MetaData

metadata = metadata (engine)

4. Defining tables
 fromSQLAlchemyImportTable, Column, Integer, String, ForeignKey, Sequence
Users= Table ('Users', metadata, Column ('ID', Integer, Sequence ('User_id_seq'), primary_key=True), Column ('name', String), Column ('FullName', String),) addresses= Table ('addresses', metadata, Column ('ID', Integer, primary_key=True), Column ('user_id', None, ForeignKey ('users.id')), Column ('email_address', String, nullable=False))

5. Create a table
# Metadata.drop_all ()
Metadata.create_all ()

6. Insert
#Way OneINS = Users.insert (). VALUES (name='Jack', fullname='Jack Jones .')conn.execute (INS)#Mode twoConn.execute (Users.insert (), id=2, name='Wendy', fullname='Wendy Williams')#Way ThreeConn.execute (Addresses.insert (), [{'user_id': 1,'email_address':'[email protected]'},    {'user_id': 1,'email_address':'[email protected]'},    {'user_id': 2,'email_address':'[email protected]'},    {'user_id': 2,'email_address':'[email protected]'}, ])

7. Enquiry
 fromSqlalchemy.sqlImportSelect
forRowinchconn.execute (select ([Users])):Print("Name:", Row[users.c.name],"; FullName:", Row[users.c.fullname]) forRowinchconn.execute (select ([Users, Addresses]):Print(Row) forRowinchConn.execute (select ([Users, Addresses]). WHERE (users.c.id = =addresses.c.user_id)):Print(Row) fromSqlalchemy.sqlImportAnd_, Or_, not_s= Select ([(Users.c.fullname +", "+addresses.c.email_address). Label ('title')]). Where (And_ (users.c.id==addresses.c.user_id, Users.c.name.between ('m','Z'), Or_ (Addresses.c.email_address.like ('% @aol. com'), Addresses.c.email_address.like ('% @msn. com'))) Conn.execute (s). Fetchall ()

8. Complete code
#Binding Database fromSQLAlchemyImportCreate_engineengine= Create_engine ('sqlite:///:memory:', echo=True)#connecting to a databaseconn =Engine.connect ()#Meta Data fromSQLAlchemyImportMetadatametadata=MetaData (engine)#Defining Tables fromSQLAlchemyImportTable, Column, Integer, String, ForeignKey, Sequenceusers= Table ('Users', metadata, Column ('ID', Integer, Sequence ('User_id_seq'), primary_key=True), Column ('name', String), Column ('FullName', String),) addresses= Table ('addresses', metadata, Column ('ID', Integer, primary_key=True), Column ('user_id', None, ForeignKey ('users.id')), Column ('email_address', String, nullable=False))#Create a table
# Metadata.drop_all ()
Metadata.create_all ()#Insert#Way OneINS = Users.insert (). VALUES (name='Jack', fullname='Jack Jones .') Result=conn.execute (INS)#Mode twoConn.execute (Users.insert (), id=2, name='Wendy', fullname='Wendy Williams')#Way ThreeConn.execute (Addresses.insert (), [{'user_id': 1,'email_address':'[email protected]'}, {'user_id': 1,'email_address':'[email protected]'}, {'user_id': 2,'email_address':'[email protected]'}, {'user_id': 2,'email_address':'[email protected]'}, ])#Enquiry fromSqlalchemy.sqlImportSelect
forRowinchconn.execute (select ([Users])):Print("Name:", Row[users.c.name],"; FullName:", Row[users.c.fullname]) forRowinchconn.execute (select ([Users, Addresses]):Print(Row) forRowinchConn.execute (select ([Users, Addresses]). WHERE (users.c.id = =addresses.c.user_id)):Print(Row) fromSqlalchemy.sqlImportAnd_, Or_, Not_
S= Select ([(Users.c.fullname +", "+addresses.c.email_address). Label ('title')]). Where (And_ (users.c.id==addresses.c.user_id, Users.c.name.between ('m','Z'), Or_ (Addresses.c.email_address.like ('% @aol. com'), Addresses.c.email_address.like ('% @msn. com'))) Conn.execute (s). Fetchall ()



Example of using Core mode of SQLAlchemy

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.