1. 安装PyMySQL

pip install PyMySQL

关于PyMySQL的详细内容可以查看官方文档  Github

2. 创建表

在某个数据库内,使用以下指令建表

CREATE TABLE `users`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8 COLLATE=utf8_bin

3. 使用PyMySQL执行增删改查

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019-1-7 15:43
# @Author : Z.C.Wang
# @Email : iwangzhengchao@gmail.com
# @File : PyConnectionMySQL.py
# @Software: PyCharm Community Edition
"""
Description :
pymysql.Connect()参数说明
host(str): MySQL服务器地址
port(int): MySQL服务器端口号
user(str): 用户名
passwd(str): 密码
db(str): 数据库名称
charset(str): 连接编码 connection对象支持的方法
cursor() 使用该连接创建并返回游标
commit() 提交当前事务
rollback() 回滚当前事务
close() 关闭连接 cursor对象支持的方法
execute(sql, args) 执行一个数据库的查询命令
fetchone() 取得结果集的下一行
fetchmany(size) 获取结果集的下几行
fetchall() 获取结果集中的所有行
close() 关闭游标对象
"""
import pymysql # 连接数据库
connection = pymysql.connect(host='localhost', user='root', password='root',
db='test', charset='utf8') if connection.open:
print('the connection is open...') # 清空users表
cursor = connection.cursor()
cursor.execute("truncate table users")
connection.commit() # (1)批量插入
record = []
for i in range(10):
email = "mail_" + str(i) + "@qq.com"
password = "xxx_" + str(i)
record.append((email, password)) try:
sql = "insert into users (email, password) values (%s, %s)"
rows = cursor.executemany(sql, record)
connection.commit()
print("insert success. affected rows : %d" % rows)
except:
print("insert ERROR.")
connection.rollback() # (2)删除记录
try:
sql = "delete from users where id=1"
rows = cursor.execute(sql)
connection.commit()
print("delete success. affected rows : %d" % rows)
except:
print("delete ERROR.")
connection.rollback() # (3)修改记录
try:
sql = "update users set password='yyy' where id=5"
rows = cursor.execute(sql)
connection.commit()
print("update success. affected rows : %d" % rows)
except:
print("update ERROR.")
connection.rollback() # (4)查询记录
try:
sql = 'select * from users'
count = cursor.execute(sql)
print("number of record in users: %d" % count)
result = cursor.fetchall()
for row in result:
print(row)
connection.commit()
except:
print("query ERROR.")
connection.rollback() # 关闭连接
cursor.close()
connection.close()
print("connection close.")

运行结果:

D:\Python3.6_2\python.exe E:/PycharmProjects/PyConnectionMySQL/PyConnectionMySQL.py
the connection is open...
insert success. affected rows : 10
delete success. affected rows : 1
update success. affected rows : 1
number of record in users: 9
(2, 'mail_1@qq.com', 'xxx_1')
(3, 'mail_2@qq.com', 'xxx_2')
(4, 'mail_3@qq.com', 'xxx_3')
(5, 'mail_4@qq.com', 'yyy')
(6, 'mail_5@qq.com', 'xxx_5')
(7, 'mail_6@qq.com', 'xxx_6')
(8, 'mail_7@qq.com', 'xxx_7')
(9, 'mail_8@qq.com', 'xxx_8')
(10, 'mail_9@qq.com', 'xxx_9')
connection close. Process finished with exit code 0

Python3使用PyMySQL操作数据库的更多相关文章

  1. Python学习(二十九)—— pymysql操作数据库优化

    转载自:http://www.cnblogs.com/liwenzhou/articles/8283687.html 我们之前使用pymysql操作数据库的操作都是写死在视图函数中的,并且很多都是重复 ...

  2. pymysql操作数据库、索引、慢日志管理

    目录 pymysql操作数据库 简单操作 sql的注入问题 sql注入问题解决办法 sql注入问题模板总结 利用pymysql操作数据库 (增删改),conn.commit() 索引 1.为何要有索引 ...

  3. pymysql 操作数据库

    一.简介 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同,但目前pymysql支持python3.x而后者不支持3.x版本 其执行语句与sql源码相似 二.使用 ...

  4. Python使用PyMysql操作数据库

    安装 pip install -U pymysql 连接数据库 连接数据库有两种不同的格式 直接使用参数 代码如下 import pymysql.cursors connection = pymysq ...

  5. 使用pymysql操作数据库

    学习如何使用python的pymysql模块来操作mysql数据库 这里的基本用法主要借鉴了该篇博客:https://www.cnblogs.com/woider/p/5926744.html 因为这 ...

  6. 用pymysql操作数据库

    import pymysql # 打开数据库连接 connection = pymysql.connect(host='127.0.0.1', user='root', passwd=', db='s ...

  7. MySQL-注释-Navicat基本使用-复杂查询练习题-解题思路-pymysql操作数据库-SQL注入-05

    目录 mysql语句注释 navicat 的基本使用 特色(个人总结) 与数据服务器建立连接 创建&打开数据库.表 创建 打开 修改操作表结构 修改表结构 查询修改操作表数据 基本语句对应的操 ...

  8. pymysql操作数据库

    pymysql.connect()参数说明:(连接数据库时需要添加的参数)host(str): MySQL服务器地址port(int): MySQL服务器端口号user(str): 用户名passwd ...

  9. python(pymysql操作数据库)

    第一种方式 import pymysql # 打开数据库连接 db = pymysql.connect(host="192.168.88.11", user="root& ...

随机推荐

  1. 【Unity3d】3d角色换装实现原理及步骤

    http://www.cnblogs.com/dosomething/archive/2012/04/15/2450526.html 1.角色模型制作 unity3d支持Skin动画  但是不支持Ph ...

  2. adb相关

    --------------------------------------------- adb logcat |find "nafio" >c:/logcat.txt 另 ...

  3. IT兄弟连 Java Web教程 Web开发的相关知识

    Web基本概念 Web,是环球信息网的缩写,也称作“WWW.W3”,英文全称为World Wide Web,中文名成为万维网,常简称为Web.Web分为Web客户端和Web服务器程序.Web可以让We ...

  4. iperf工具学习记录

    源码下载地址:http://sourceforge.net/projects/iperf/ 编译命令: tar -zxvf iperf-2.0.5.tar.gz cd iperf-2.0.5 ./co ...

  5. mac的日常使用总结

    目录 有一个github的仓库:(强烈推荐) 不推荐的但是可以试试的一些链接: # 关于mac book的使用教程 github简直是一个宝藏,发现好多各种好玩的东西, 爱了爱了, 开源一定是未来, ...

  6. C#中自定义类数组和结构数组的使用

    如有雷同,不胜荣幸,若转载,请注明 C#中自定义类数组和结构数组的使用 最近在很多项目中发现很多时候给定的数组要实现某个逻辑或处理很是麻烦,一维数组,二维数组,,,等等需要经过n多转换,还不如自己写一 ...

  7. Reduce实现

    Reduce实现 参考 第一版 Array.prototype.fakeReduce = function (fn, base) { // this 指向原数组 // 拷贝数据, 更改指针方向 var ...

  8. 金蝶Apusic中间件适配JetSpeed2过程记录

    金蝶Apusic中间件适配JetSpeed2过程记录: 1.安装金蝶并配置域,确保域运行正常. 2.参考<JetSpeed2部署至Apusic操作步骤记录>进行应用迁移. https:// ...

  9. No space left on device

    No space left on device 数据库无法启动, 发现是内存没有清空导致. 处理过程: ipcs ipcrm  

  10. HAL之串口

    在STM32cubeMX中 1 外设功能打开 2 GPIO对应管脚的串口功能打开 3 对应GPIO引脚的配置 4串口的配置,中断的设置 在MDK中 5.1 串口初始化MX_USART1_UART_In ...