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)的更多相关文章

  1. mysql数据库----python操作mysql ------pymysql和SQLAchemy

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...

  2. day40:python操作mysql:pymysql模块&SQL注入攻击

    目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...

  3. Python操作MySQL之SQLAlchemy

      SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结 ...

  4. Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy

    Mysql基础 一.安装 Windows: 1.下载 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-winx64.zip 2.解压 ...

  5. Python操作MySQL:pymysql和SQLAlchemy

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  6. python成长之路【第十三篇】:Python操作MySQL之pymysql

    对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎 ...

  7. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  8. Python 操作 MySQL 之 pysql 与 ORM(转载)

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  9. Python开发【第十九篇】:Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  10. Day12(补充) Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

随机推荐

  1. 【费用流】【网络流24题】【P1251】 餐巾计划问题

    Description 一个餐厅在相继的 \(N\) 天里,每天需用的餐巾数不尽相同.假设第 \(i\) 天需要 \(r_i\)块餐巾.餐厅可以购买新的餐巾,每块餐巾的费用为 \(p\) 分;或者把旧 ...

  2. C#线程篇---Windows调度线程准则(3)

    Windows本身就是一个抢占式操作系统,它的实现,必定有某种算法在里面,比如什么时候调度哪些线程,需要花费多长时间等问题. 我们时时在用Windows,作为程序员,我们有必要知道其中最贴近我们的算法 ...

  3. cocoaPods安装、更新第三方库

    pod install 换成 pod install --verbose --no-repo-update pod update 换成 pod update --verbose --no-repo-u ...

  4. 最短路径算法的实现(dijskstra):Python

    dijskstra最短路径算法步骤: 输入:图G=(V(G),E(G))有一个源顶点S和一个汇顶点t,以及对所有的边ij属于E(G)的非负边长出cij. 输出:G从s到t的最短路径的长度. 第0步:从 ...

  5. Java多态性的“飘渺之旅”

    原文出处:斯武丶风晴 摘要: 如何从Java多态性进行飘渺之旅呢? 我们用例子来旅行. 朵星人A:人类,是一个很奇妙的物种. 朵星人B:他们好像分为两种,嗯 先生,以及美女? 朵星人C:对,更年轻的有 ...

  6. numpy二进制转换和范围缩放

    numpy二进制转换和范围缩放 觉得有用的话,欢迎一起讨论相互学习~Follow Me 一维二进制转换 import numpy as np # 一维二进制数组转换 a=np.array([0,1,1 ...

  7. 转:CocoaPods pod install/pod update更新慢的问题

    最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...

  8. Mongodb 笔记06 副本集的组成、从应用程序连接副本集、管理

    副本集的组成 1. 同步:MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作.oplog是主节点的local数据库中的一个固定集合.备份节点通过查询整个集合就可 ...

  9. python安装包提示error: option --single-version-externally-managed not recognized

    pip install mysql-connector-python-rf==2.2.2 安装包的时候提示错误信息:error:option--single-version-externally-ma ...

  10. 2015/11/7用Python写游戏,pygame入门(7):碰撞检测

    我们已经完成了飞机大战的大部分东西,但是游戏还是没有办法正式开玩,因为子弹并不能打掉飞机.只有完成了这一个工作,游戏才算基本成型. 今天的内容就非常简单了,就是做到这个碰撞检测,以及控制好子弹和飞机的 ...