标签:
1.安装驱动
输入命令:pip install MySQL-python
2.直接使用驱动
#coding=utf-8 import MySQLdb conn= MySQLdb.connect( host=‘127.0.0.1‘, port = 3306, user=‘root‘, passwd=‘root‘, db =‘数据库名称‘, charset=‘utf8‘ ) cur = conn.cursor() aa=cur.execute("select * from 表名") print aa
3.ORM框架——SQLAlchemy
输入命令:pip install Flask-SQLAlchemy
快速入门:http://www.pythondoc.com/flask-sqlalchemy/quickstart.html
from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘mysql://root:root@localhost/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 = Age inset=User(Name=‘cnki‘,Age=25) db.session.add(inset) db.session.commit()
标签:
原文地址:http://www.cnblogs.com/cnki/p/5697405.html