Python3使用PyMySQL操作数据库
1. 安装PyMySQL
pip install PyMySQL
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操作数据库的更多相关文章
- Python学习(二十九)—— pymysql操作数据库优化
转载自:http://www.cnblogs.com/liwenzhou/articles/8283687.html 我们之前使用pymysql操作数据库的操作都是写死在视图函数中的,并且很多都是重复 ...
- pymysql操作数据库、索引、慢日志管理
目录 pymysql操作数据库 简单操作 sql的注入问题 sql注入问题解决办法 sql注入问题模板总结 利用pymysql操作数据库 (增删改),conn.commit() 索引 1.为何要有索引 ...
- pymysql 操作数据库
一.简介 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同,但目前pymysql支持python3.x而后者不支持3.x版本 其执行语句与sql源码相似 二.使用 ...
- Python使用PyMysql操作数据库
安装 pip install -U pymysql 连接数据库 连接数据库有两种不同的格式 直接使用参数 代码如下 import pymysql.cursors connection = pymysq ...
- 使用pymysql操作数据库
学习如何使用python的pymysql模块来操作mysql数据库 这里的基本用法主要借鉴了该篇博客:https://www.cnblogs.com/woider/p/5926744.html 因为这 ...
- 用pymysql操作数据库
import pymysql # 打开数据库连接 connection = pymysql.connect(host='127.0.0.1', user='root', passwd=', db='s ...
- MySQL-注释-Navicat基本使用-复杂查询练习题-解题思路-pymysql操作数据库-SQL注入-05
目录 mysql语句注释 navicat 的基本使用 特色(个人总结) 与数据服务器建立连接 创建&打开数据库.表 创建 打开 修改操作表结构 修改表结构 查询修改操作表数据 基本语句对应的操 ...
- pymysql操作数据库
pymysql.connect()参数说明:(连接数据库时需要添加的参数)host(str): MySQL服务器地址port(int): MySQL服务器端口号user(str): 用户名passwd ...
- python(pymysql操作数据库)
第一种方式 import pymysql # 打开数据库连接 db = pymysql.connect(host="192.168.88.11", user="root& ...
随机推荐
- hdoj1789【贪心】
题意: 已知有n个作业,每个作业呢,都是一天可以做完,每个作业都有一个截止日期,每个作业如果超过他的截止日期会扣分,最后让你求一个怎么安排求得一个最小扣的分数. 比如现在有3个作业 截止日期:3 3 ...
- 求一元二次方程的根【double型的0输出%.2lf为-0.00】
#include <bits/stdc++.h> using namespace std; #define LL long long #define eps 1e-6 int main() ...
- spoj SUBST1 - New Distinct Substrings【SAM||SA】
SAM里的转台不会有重复串,所以答案就是每个right集合所代表的串个数的和 #include<iostream> #include<cstdio> #include<c ...
- linux之用户态和内核态
一. Unix/Linux的体系架构 如上图所示,从宏观上来看,Linux操作系统的体系架构分为用户态和内核态(或者用户空间和内核).内核从本质上看是一种软件——控制计算机的硬件资源,并提供上层应用程 ...
- Spark MLlib编程API入门系列之特征选择之向量选择(VectorSlicer)
不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). VectorSlicer用于从原来的特征 ...
- 十个非常棒的学习angularjs的英文网站
AngularJS 是非常棒的JS框架,能够创建功能强大,动态功能的Web app.AngularJS自2009发布以来,已经广泛应用于Web 开发中.但是对想要学习Angular JS 的人而言,只 ...
- 移动端UI自动化Appium测试——Android系统下使用uiautomator viewer查找元素
在利用Appium做自动化测试时,最重要的一步就是获取对应的元素值,根据元素来对对象进行对应的操作,如何获得对象元素呢?Appium Server Console其实提供了一个界面对话框&qu ...
- 单页Html及Android App供小孩学习常用汉字
为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...
- android开发学习 ------- json数据与实体类之间的相互转换
在网络请求的时候,会返回给我们实体类,我们需要将实体类转化为json字符串,方便处理数据: 有时候也会将json数据转换为实体类. 在Android Studio中,json要互相转换,需要用到gso ...
- abp zero mysql版正式发布
AbpZero-MySql aspnet-zero-1.12.0的mysql版本来啦.跟mssql版一样的功能,一样的代码. 获取源码