Pythonorm framework SQLAlchemy simple application (database operation) instance code

Source: Internet
Author: User
This article details the pythonorm framework SQLAlchemy simple application (database operations) instance code This article details the python orm framework SQLAlchemy simple application (database operations) instance code

# _ * _ Coding: UTF-8 _ * _ ''' basic process for creating a SQLAlchemy Application 1. create a connection (create a connection with a relational database) 2. declare a ing File (one-to-one ing between classes in python and tables in the database, so that you can operate on tables in the database using classes in python) 3. Create mode (tables can be created) 4. initialize ing instance 5. create Session 6. persistence instance object ''' from sqlalchemy import create_engine # create connection engine = create_engine ('MySQL: // root: 1qaz # EDC@192.168.89.101: 3306/student ', echo = True) ''' parameter description: mysql indicates that the database to be connected is the mysql database root, indicating that the root account is used to log on to the mysql database 1qaz # EDC indicates that the password of the root account connecting to the database @ is a symbol, in the format of 192.168.89.1.101: 3306 indicates the database connection address and port 3306 soms indicates the name of the database to be connected echo = True indicates whether to print the SQL statement during execution, false: The 'ing file from sqlalchemy is not printed. ext. declarative import declarative_base # introduces the declarative_base class Base = declarative_base () # instantiates a declarative_base instance from sqlalchemy import Column, Integer, String # introduces the class Column, Integer, string class User (Base): # Create your own instance class named User to inherit Base class _ tablename _ = 'users' # this attribute, ing id = Column (Integer, primary_key = True) # This attribute indicates that the id represents a Column in the database table, and Integer indicates an Integer, and id is the primary key name = Column (String (10) # This attribute indicates that it is also a Column in the database table, and its type is string passwd = Column (String (10 )) # This attribute indicates that the database table is also a column. its type is string def _ repr _ (self): # A method is defined, the purpose is to present the User class more vividly, and return is useless"
 
  
"% (Self. name, self. name, self. passwd) # create a database table in the creation mode. The table name is usersUser. metadata. create_all (engine) # create session ''' creation will convert ''' from sqlalchemy. orm import sessionmaker # introduce the sessionmaker function Session = sessionmaker (bind = engine) # Create a session dialog through which you can perform some operations on the database session = session () # binding # persistence of an instance object ed_user = User (id = 2, name = 'tantianran ', passwd = '1qaz # edc') # insert data sessions to columns in the database table. add (ed_user) # add this instance to the session. commit () # submit
 

Execution result:

The above is the details of the instance code of the python orm framework SQLAlchemy simple application (database operation). For more information, see other related articles in the first PHP community!

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.