Use SQLAlchemy and MySQL to build database environment and code in Python environment

Source: Internet
Author: User

To be compatible with text in Chinese and other countries, we set the encoding format to UTF8

MySQL settings UTF8 can directly modify the configuration file, or through the command line environment variable to set

SQLAlchemy setting the encoding character set be sure to add Charset=utf8 to the URL of the database access, otherwise the database connection is not a UTF8 encoding format

Eng = Create_engine (' Mysql://root:[email Protected]:3306/test2?charset=utf8 ', echo=true)

Note the later part of the URL

Charset=utf8

This is key, in addition to the above two places to set up and modify, in addition to Python files need to set the encoding format to UTF8 (without BOM), so forget the database

Directly stored data, you can directly use the ' Chinese ', or U ' Chinese ' to correctly store in the MySQL database.

MySQL database does not display Chinese correctly in DOS command window, if you want to view Chinese in UTF8 encoded format, it is best to download a MySQL visualizer.

At the moment I feel good about MySQL visualizer is sqlyog.

Here is the demo code I wrote using SQLAlchemy to write to the database, you can refer to the reference.

#coding =utf-8from sqlalchemy Import Column, String, create_enginefrom sqlalchemy.orm import Sessionmakerfrom Sqlalchemy.ext.declarative Import declarative_basebase = Declarative_base () class User (base): __tablename__ = ' user2 ' ID = Column (string), primary_key=true) name = Column (string ()) class School (Base): __tablename__ = ' School ' id = column ( String (primary_key=true), name = Column (string) #创建从Base派生的所有表def Createall (Eng): Base.metadata.create_all ( ENG) #删除DB中所有的表def Dropall (Eng): Base.metadata.drop_all (Eng) #删除数据库中从Base派生的所有表 # Create Session object If __name__ = = ' __main__ ': #创建数据库引擎eng = Create_engine (' Mysql://root:[email protected]:3306/test2?charset=utf8 ', echo=true) #设置回显 # Eng.echo = true# Create dbsession type dbsession = Sessionmaker (bind=eng) #创建session对象session = Dbsession () Try: #创建表createAll ( ENG) #创建新User对象new_user = User (id= ', name= ' I еёжжщщщьфффтххмлл ') print (U ' hello ') #添加到sessionsession. Add (New_user) # Commit Save to Database Session.commit () except Baseexception,e:print (' e.message=%s '% str (e.message)) Finally:sessioN.close () #eng. Close () 


Use SQLAlchemy and MySQL to build database environment and code in Python environment

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.