python操作mysql(pymysql + sqlalchemy)
pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。
下载安装
pip3 install pymysql
使用操作
1、执行sql
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql # 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')
# 创建游标
cursor = conn.cursor() # 执行SQL,并返回收影响行数
effect_row = cursor.execute("update hosts set host = '1.1.1.2'") # 执行SQL,并返回受影响行数
#effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,)) # 执行SQL,并返回受影响行数
#effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) # 提交,不然无法保存新建或者修改的数据
conn.commit() # 关闭游标
cursor.close()
# 关闭连接
conn.close()
2、获取新创建数据自增ID
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')
cursor = conn.cursor()
cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
conn.commit()
cursor.close()
conn.close() # 获取最新自增ID
new_id = cursor.lastrowid
3、获取查询数据
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1')
cursor = conn.cursor()
cursor.execute("select * from hosts") # 获取第一行数据
row_1 = cursor.fetchone() # 获取前n行数据
# row_2 = cursor.fetchmany(3)
# 获取所有数据
# row_3 = cursor.fetchall() conn.commit()
cursor.close()
conn.close()
注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
- cursor.scroll(1,mode='relative') # 相对当前位置移动
- cursor.scroll(2,mode='absolute') # 相对绝对位置移动
4、fetch数据类型
关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='t1') # 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute("call p1()") result = cursor.fetchone() conn.commit()
cursor.close()
conn.close()
一、对象映射关系(ORM)
orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了保证一致的使用习惯,通过orm将编程语言的对象模型和数据库的关系模型建立映射关系,这样我们在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了,而不用直接使用sql语言
优点:
- 隐藏了数据访问细节,“封闭”的通用数据库交互,ORM的核心。他使得我们的通用数据库交互变得简单易行,并且完全不用考虑该死的SQL语句。快速开发,由此而来
- ORM使我们构造固化数据结构变得简单易行
缺点:
- 无可避免的,自动化意味着映射和关联管理,代价是牺牲性能(早期,这是所有不喜欢ORM人的共同点)。现在的各种ORM框架都在尝试使用各种方法来减轻这块(LazyLoad,Cache),效果还是很显著的
二、SQLAlchemy
在Python中,最有名的ORM框架是SQLAlchemy。用户包括openstack\Dropbox等知名公司或应用
Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,SQLAlchemy本身无法操作数据库,其必须以来pymsql等第三方插件,Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,如:
MySQL-Python
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname> pymysql
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>] MySQL-Connector
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname> cx_Oracle
oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...] 更多详见:http://docs.sqlalchemy.org/en/latest/dialects/index.html
安装:
pip install SQLAlchemy
pip install pymysql
一、内部处理
使用 Engine/ConnectionPooling/Dialect 进行数据库操作,Engine使用ConnectionPooling连接数据库,然后再通过Dialect执行SQL语句。
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy import create_engine engine = create_engine("mysql+pymysql://root:xiaoming.note5@115.159.193.77:3306/school?charset=utf8", max_overflow=5) # 执行SQL
cur = engine.execute(
"insert into user (name, password) values('lihy', 'lihy')"
) # 新插入行自增ID
cur.lastrowid # 执行SQL
cur = engine.execute(
"insert into user(name, password) values(%s, %s)", [('liq', 'liq'), ('liuxj', 'liuxj235')]
) # 执行SQL
cur = engine.execute(
"insert into user(name, password) values(%(name)s, %(password)s)", name='lium', password='lium123'
) # 执行SQL
cur = engine.execute('select * from user') # 获取第一行数据, 第n行,所有数据
cur.fetchone()
cur.fetchmany(3)
cur.fetchall()
二、ORM功能使用
使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。
1、外键关联
创建表
# orm_fk.py
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, Date
from sqlalchemy.orm import relationship engine = create_engine("mysql+pymysql://root:xiaoming.note5@115.159.193.77/school", encoding='utf-8')
Base = declarative_base() class Student(Base):
__tablename__ = 'student'
id = Column(Integer, primary_key=True)
name = Column(String(32), nullable=False)
age = Column(String(32), nullable=False)
register_date = Column(Date, nullable=False)
def __repr__(self):
return '<%s name:%s>' % (self.id, self.name) class StudyRecord(Base):
__tablename__ = 'study_record'
id = Column(Integer, primary_key=True)
day = Column(Integer,nullable=False)
status = Column(String(32), nullable=False)
stu_id = Column(Integer, ForeignKey('student.id'))
#关联student表里的id
student = relationship('Student', backref='my_study_record') # Student为关联的类 def __repr__(self):
return '<%s day:%s status:%s>' % (self.student.name, self.day, self.status) Base.metadata.create_all(engine)
注:my_student = relationship("Student",backref="my_study_record")这个nb,允许你在user表里通过backref字段反向查出所有它在addresses表里的关联项
插入数据
# cat orm_fk
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from orm_fk import Student, StudyRecord, engine Session = sessionmaker(bind=engine)
session = Session() session.add_all([
Student(name='lihy', age=21, register_date='2016-10-15'),
Student(name='liq', age=22, register_date='2016-11-16'),
Student(name='zhuxj', age=23, register_date='2016-12-17'),
StudyRecord(day=1, status='yes', stu_id=1),
StudyRecord(day=2, status='yes', stu_id=1),
StudyRecord(day=3, status='no', stu_id=1),
StudyRecord(day=3, status='yes', stu_id=2),
])
session.commit() st1 = Student(name='lium', age=22, register_date='2011-10-15')
st2 = Student(name='liuxj', age=25, register_date='2011-11-15')
sr1 = StudyRecord(day=4, status='yes', stu_id=1),
sr2 = StudyRecord(day=5, status='yes', stu_id=1),
sr3 = StudyRecord(day=6, status='no', stu_id=1),
sr4 = StudyRecord(day=7, status='yes', stu_id=2),
session.add_all([st1,st2,sr1,sr2,sr3,sr4])
session.commit()
查询数据
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from orm_fk import Student, StudyRecord, engine Session = sessionmaker(bind=engine)
session = Session() stu_obj = session.query(Student).filter(Student.name=='lihy').first()
print(stu_obj.my_study_record)
2、多外键关联
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, Date
from sqlalchemy.orm import relationship engine = create_engine("mysql+pymysql://root:xiaoming.note5@115.159.193.77/school", encoding='utf-8')
Base = declarative_base() class Customer(Base):
__tablename__ = 'customer'
id = Column(Integer, primary_key=True)
name = Column(String(32)) billing_address_id = Column(Integer, ForeignKey('address.id'))
shipping_address_id = Column(Integer, ForeignKey('address.id')) billing_address = relationship('Address', foreign_keys=[billing_address_id])
shipping_address = relationship('Address', foreign_keys=[shipping_address_id]) def __repr__(self):
return '<%s name:%s billing_address:%s shipping_adress>' % (self.name, self.billing_address.street, self.shipping_address.street) class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
street = Column(String(64))
city = Column(String(64))
province = Column(String(64)) Base.metadata.create_all(engine)
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Address, Customer, engine Session = sessionmaker(bind=engine)
session = Session() session.add_all([
Address(street='huaxia', city='SH', province='ShangHai'),
Address(street='sunhua', city='BJ', province='HeNan'),
Address(street='xihuan', city='XC', province='ShangHai'),
Customer(name='lihy', billing_address_id=1, shipping_address_id=2),
Customer(name='liq', billing_address_id=1, shipping_address_id=1),
]) session.commit()
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Customer, Address, engine Session = sessionmaker(bind=engine)
session = Session() ret = session.query(Customer).filter(Customer.name=='lihy').first()
print(ret.billing_address.street, ret.shipping_address.province)
3、多对多关联
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, Date, Table
from sqlalchemy.orm import relationship engine = create_engine("mysql+pymysql://root:xiaoming.note5@115.159.193.77/school", encoding='utf-8')
Base = declarative_base() bookidToAuthorid = Table('bookidToAuthorid', Base.metadata,
Column('bookid', Integer, ForeignKey('books.id')),
Column('authorid', Integer, ForeignKey('authors.id')),
)
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
name = Column(String(64))
pub_date = Column(Date)
authors = relationship('Author', secondary=bookidToAuthorid, backref='books') def __repr__(self):
return self.name class Author(Base):
__tablename__ = 'authors'
id = Column(Integer, primary_key=True)
name = Column(String(32)) def __repr__(self):
return self.name Base.metadata.create_all(engine)
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine Session = sessionmaker(bind=engine)
session = Session() b1 = Book(name="learn python", pub_date='2011-10-15')
b2 = Book(name="learn linux", pub_date='2011-10-16')
b3 = Book(name="learn C++", pub_date='2011-10-17') a1 = Author(name="lihy")
a2 = Author(name="liq")
a3 = Author(name="lium") b1.authors = [a1, a3]
b3.authors = [a1, a2, a3] session.add_all([b1, b2, b3, a1, a2, a3])
session.commit()
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine Session = sessionmaker(bind=engine)
session = Session() ret = session.query(Book).filter(Book.name=='learn python').first()
print(ret.authors)
多对多删除
通过书删除作者
未删前:
[root@VM_255_164_centos mtm]# python3 query.py
[lihy, lium]
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine Session = sessionmaker(bind=engine)
session = Session() author_obj = session.query(Author).filter(Author.name=='lihy').first()
book_obj = session.query(Book).filter_by(name="learn python").first() book_obj.authors.remove(author_obj)
session.commit()
# 删除后
# python3 query.py
[lium]
直接删除作者,会把这个作者跟所有书的关联数据也删掉
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine Session = sessionmaker(bind=engine)
session = Session() author_obj = session.query(Author).filter(Author.name=='lihy').first() session.delete(author_obj)
session.commit()
查询数据
mysql> select * from books;
+----+--------------+------------+
| id | name | pub_date |
+----+--------------+------------+
| 1 | learn python | 2011-10-15 |
| 2 | learn C++ | 2011-10-17 |
| 3 | learn linux | 2011-10-16 |
+----+--------------+------------+
3 rows in set (0.00 sec) print(session.query(Book.name, Book.pub_date).all())
# [('learn python', datetime.date(2011, 10, 15)), ('learn C++', datetime.date(2011, 10, 17)), ('learn linux', datetime.date(2011, 10, 16))]
多条件查询
objs = session.query(Book).filter(Book.id>1).filter(Book.id<3).all()
统计
session.query(Book).filter(Book.name.like('l%')).count()
分组
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine
from sqlalchemy import func Session = sessionmaker(bind=engine)
session = Session() print(session.query(func.count(Book.name), Book.name).group_by(Book.name).all())
# [(1, 'learn C++'), (1, 'learn linux'), (1, 'learn python')]
相当于原声sql:
mysql> select count(books.name) AS count_1, books.name as books_name from books group by books.name;
+---------+--------------+
| count_1 | books_name |
+---------+--------------+
| 1 | learn C++ |
| 1 | learn linux |
| 1 | learn python |
+---------+--------------+
3 rows in set (0.00 sec)
修改
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine
from sqlalchemy import func Session = sessionmaker(bind=engine)
session = Session() books_obj = session.query(Book).filter_by(name='learn python').first()
print(books_obj.pub_date)
books_obj.pub_date = "2011-11-11"
session.commit()
print(books_obj.pub_date) # python3 d1.py
2011-10-15
2011-11-11
回滚
#!/usr/bin/env python
# coding=utf-8 from sqlalchemy.orm import sessionmaker
from cj import Book, Author, engine
from sqlalchemy import func Session = sessionmaker(bind=engine)
session = Session() books_obj = session.query(Book).filter_by(name='learn python').first()
print(books_obj.pub_date)
books_obj.pub_date = "2012-12-12"
print(books_obj.pub_date)
session.rollback()
print(books_obj.pub_date) # 2011-11-11
# 2012-12-12
# 2011-11-11
其他:
# 删
session.query(Book).filter(Book.id > 2).delete()
session.commit() # 增
session.query(Book).filter(Book.id == 2).update({"pub_date": "2013-12-13"})
session.commit()
session.query(Book).filter(Book.id == 2).update({Book.pub_date: Book.pub_date + 10}) # 查
session.query(Book).all() # 条件
ret = session.query(Users).filter_by(name='alex').all()
ret = session.query(Users).filter(Users.id > 1, Users.name == 'eric').all()
ret = session.query(Users).filter(Users.id.between(1, 3), Users.name == 'eric').all()
ret = session.query(Users).filter(Users.id.in_([1,3,4])).all()
ret = session.query(Users).filter(~Users.id.in_([1,3,4])).all()
ret = session.query(Users).filter(Users.id.in_(session.query(Users.id).filter_by(name='eric'))).all()
from sqlalchemy import and_, or_
ret = session.query(Users).filter(and_(Users.id > 3, Users.name == 'eric')).all()
ret = session.query(Users).filter(or_(Users.id < 2, Users.name == 'eric')).all()
ret = session.query(Users).filter(
or_(
Users.id < 2,
and_(Users.name == 'eric', Users.id > 3),
Users.extra != ""
)).all() # 通配符
ret = session.query(Users).filter(Users.name.like('e%')).all()
ret = session.query(Users).filter(~Users.name.like('e%')).all() # 限制
ret = session.query(Users)[1:2] # 排序
ret = session.query(Users).order_by(Users.name.desc()).all()
ret = session.query(Users).order_by(Users.name.desc(), Users.id.asc()).all() # 分组
from sqlalchemy.sql import func ret = session.query(Users).group_by(Users.extra).all()
ret = session.query(
func.max(Users.id),
func.sum(Users.id),
func.min(Users.id)).group_by(Users.name).all() ret = session.query(
func.max(Users.id),
func.sum(Users.id),
func.min(Users.id)).group_by(Users.name).having(func.min(Users.id) >2).all() # 连表 ret = session.query(Users, Favor).filter(Users.id == Favor.nid).all() ret = session.query(Person).join(Favor).all() ret = session.query(Person).join(Favor, isouter=True).all() # 组合
q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union(q2).all() q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union_all(q2).all()
python操作mysql(pymysql + sqlalchemy)的更多相关文章
- mysql数据库----python操作mysql ------pymysql和SQLAchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...
- day40:python操作mysql:pymysql模块&SQL注入攻击
目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...
- Python操作MySQL之SQLAlchemy
SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结 ...
- Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy
Mysql基础 一.安装 Windows: 1.下载 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-winx64.zip 2.解压 ...
- Python操作MySQL:pymysql和SQLAlchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- python成长之路【第十三篇】:Python操作MySQL之pymysql
对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎 ...
- Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- Python 操作 MySQL 之 pysql 与 ORM(转载)
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- Python开发【第十九篇】:Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- Day12(补充) Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
随机推荐
- [CF850F] Rainbow Balls
题目大意 这里 题解 我们枚举最后剩下的球的种类,那么其他球可以看做没用了. 设选定的球有\(a_i\)个,球的总数为\(s=\sum_{i=1}^n a_i\). 现在问题变为:在一个长度为\(s\ ...
- Linux下cp ~中关于“~”的疑问
目的:将wi主文件夹下的.bashrc复制到/tmp,并更名为bashrc.对于书上的代码上加“~”存在疑问. cp ~/.bashrc /tmp/bashrc 对命令进行了以下尝试: 为什么不加“~ ...
- 《Linux内核设计与实现》第18章读书笔记
第十八章 调试 一.调试开始前的准备 1.准备开始 bug 藏匿bug的版本 相关内核代码的知识 成功调试的关键在于能否将错误重现 2.内核中的bug 其产生原因无数,表象变化也多种多样.从隐藏在源代 ...
- 编译安装haproxy开启支持SSL
1.下载程序包 # wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz # tar xvf haproxy-1.7.5. ...
- WEB入门.九 导航菜单
学习内容 水平导航菜单 垂直导航菜单 下拉式导航菜单 能力目标 制作tab标签导航菜单 制作带箭头的导航菜单 制作带信息提示的导航菜单 制作垂直下拉导航菜单 制作水平下拉导航菜单 本章简介 上一章节中 ...
- 单点登录(十)-----遇到问题-----cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed
cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed. 完整报错信息: 二月 08, 2017 5 ...
- Codeforces 914F. Substrings in a String(bitset)
比赛的时候怎么没看这题啊...血亏T T 对每种字符建一个bitset,修改直接改就好了,查询一个区间的时候对查询字符串的每种字符错位and一下,然后用biset的count就可以得到答案了... # ...
- bzoj 4199 && NOI 2015 品酒大会
一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒师 Rainbow 调制了 ...
- win7,Ubuntu 12.04 双系统修改启动项顺序三方法
修改启动项顺序的三种方法 本文所涉及的方法都是在Ubuntu的安装时将引导加载程序grub安装在了整个硬盘(即MBR内),即开机以grub引导. 方法1在Ubuntu终端下输入:sudo mv /et ...
- php中文网在这里给大家汇总了最热门最全面的php面试题
PHP面试题汇总 视频版: 1.<最新PHP面试视频教程> http://www.php.cn/course/876.html 2.<PHP经典算法面试题> http://ww ...