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:[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 = Age inset=User(Name=‘cnki‘,Age=25) db.session.add(inset) db.session.commit()
时间: 2024-10-27 18:35:49