1. Install the driver
Input command: Pip install Mysql-python
2. Direct use of the drive
#coding =utf-8import mysqldbconn= mysqldb.connect ( host= ' 127.0.0.1 ', port = 3306, user= ' root ', passwd= ' root ', db = ' database name ', charset= ' UTF8 ' ) cur = conn.cursor () aa=cur.execute ("SELECT * from table name") Print AA
3.ORM Frame--sqlalchemy
Input command: Pip install Flask-sqlalchemy
Quick Start: http://www.pythondoc.com/flask-sqlalchemy/quickstart.html
From flask import Flaskfrom flask.ext.sqlalchemy Import Sqlalchemyapp = Flask (__name__) app.config[' Sqlalchemy_database _uri '] = ' mysql://root:[email protected]/test ' #app. Config.setdefault (' sqlalchemy_track_modifications ', True) db = SQLAlchemy (APP) class User (db). Model): ID = db. Column (db. Integer, primary_key=true) Name = db. Column (db. String) Age = db. Column (db. Integer) def __init__ (self,name,age): Self . Name = name Self . Age = Ageinset=user (name= ' Cnki ', age=25) Db.session.add (inset) db.session.commit ()
The MySQL operation of Python