【Python3之pymysql模块】
一、什么是 PyMySQL?
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。
二、PyMySQL 安装
pip3 install PyMySQL
三、使用操作
#!/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()
注:当使用字符串格式化时容易引起sql注入
sql = 'select * from userinfo where username="%s" and password="%s" ' %(user,pwd,) #不推荐使用
sql = 'select * from userinfo where username=%s and password=%s',[user,pwd] #推荐使用,mysql会自动格式化,避免SQL注入
import pymysql
user = input('请输入用户名:')
pwd = input('请输入密码:')
# 获取数据
conn = pymysql.Connect(host='192.168.12.89',port=3306,user='root',password="123",database="s17day11db",charset='utf8')
cursor = conn.cursor()
v = cursor.execute('select * from userinfo where username=%s and password=%s',[user,pwd])
result = cursor.fetchone()
cursor.close()
conn.close()
print(result)
- 获取新创建数据自增ID
new_class_id = cursor.lastrowid # 获取新增数据自增ID
import pymysql # 获取数据
conn = pymysql.Connect(host='192.168.12.89',port=3306,user='root',password="123",database="s17day11db",charset='utf8')
cursor = conn.cursor() cursor.execute('insert into class(caption) values(%s)',['新班级'])
conn.commit()
new_class_id = cursor.lastrowid # 获取新增数据自增ID cursor.execute('insert into student(sname,gender,class_id) values(%s,%s,%s)',['李杰','男',new_class_id])
conn.commit() cursor.close()
conn.close()
- 查询语句
#!/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') # 相对绝对位置移动
- 更改fetch数据类型
默认获取的数据是元祖类型,如果想要或者字典类型的数据
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#!/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)
v = cursor.execute('SELECT * FROM score')
result = cursor.fetchall()
# result = cursor.fetchone()
# result = cursor.fetchmany()
print(result) cursor.close()
conn.close()
【Python3之pymysql模块】的更多相关文章
- python3使用pymysql模块,连接mysql数据库,实现新增、查询和更新操作
1.环境数据准备: python3环境.pymysql模块 mysql数据库:本次代码中用到的数据库为本地的testdb数据库,user表(表字段比较简单,只有主键id,手机号mobile,密码pas ...
- python3之pymysql模块
1.python3 MySQL数据库链接模块 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Pyt ...
- python3中pymysql模块安装及连接数据库(同‘python安装HTMLTestRunner’)
https://pypi.org/project/PyMySQL/#files 安装完成之后就是连接数据库了 机器上安装了mysql的数据库,并且已经创建了两张表用于测试 python3连接数据库及删 ...
- 3-Ubuntu下python3安装pymysql模块(1)
命令:sudo pip3 install pymysql
- python3中pymysql模块的事务操作
try: cursor.execute(sql_1) cursor.execute(sql_2) cursor.execute(sql_3) except Exception a ...
- Python全栈 MySQL 数据库 (引擎、事物、pymysql模块、orm)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 存储引擎(处理表的处理器) 基本操作: ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python实战第一天-pymysql模块并练习
操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装pymysql模 ...
- python如何使用pymysql模块
Python 3.x 操作MySQL的pymysql模块详解 前言pymysql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而M ...
随机推荐
- 使用Post方法模拟登陆爬取网页
最近弄爬虫,遇到的一个问题就是如何使用post方法模拟登陆爬取网页.下面是极简版的代码: import java.io.BufferedReader; import java.io.InputStre ...
- 详解C# Tuple VS ValueTuple(元组类 VS 值元组)
C# 7.0已经出来一段时间了,大家都知道新特性里面有个对元组的优化,并且网上也有大量的介绍,这里利用详尽的例子详解Tuple VS ValueTuple(元组类VS值元组),10分钟让你更了解Val ...
- 深入理解Java常用类-----时间日期
除了String这个类在日常的项目中比较常用之外,有关时间和日期的操作也是经常遇到的,本篇就讲详细介绍下Java API中对时间和日期的支持.其实在Java 8之前时间日期的API并不是很好用,以至于 ...
- 7.ReadWriteLock接口及其实现ReentrantReadWriteLock
Java并发包的locks包里的锁基本上已经介绍得差不多了,ReentrantLock重入锁是个关键,在清楚的了解了同步器AQS的运行机制后,实际上再分析这些锁就会显得容易得多,这章节主讲另外一个重要 ...
- ReactNative学习之css样式使用
前言: 前面学习了html,今天学习一下css的基本使用,看下html与css之间是如何结合来编写前端网页的. CSS 是什么? CSS 是 Cascading Style Sheets(级联样式表) ...
- PHP实现无限级分类
前段时间做一个小项目的时候用到了无限级分类,当时也忘的差不多了,于是就去网上查资料学,下面是我的学习笔记 数据格式: array (size=5) 1 => array (size=6) 'id ...
- [codeforces631E]Product Sum
E. Product Sum time limit per test: 1 second memory limit per test: 256 megabytes input:standard inp ...
- github--hello,world(参考官网)
官网:https://guides.github.com/activities/hello-world/ 一共分为5步. 1.为你的项目新建仓库(repository): 2.新建分支(branch) ...
- 浏览器内核 网址分解 web服务器
浏览器最核心的部分是渲染引擎(Rendering Engine),我们一般习惯称之为"浏览器内核",其负责解析网页语法(如标准通用标记语言的子集HTML.JavaScript)并渲 ...
- 单机部署 kubernets 方法汇总
#minikube : 可以方便的在本机用虚拟机创建一个开箱即用的Kubernetes集群 #kubeadm : 可以自动化的将多台Ubuntu或者CentOS主机组建成集群 #nanokube,ki ...