【python】-- pymsql 操作MySQL
pymysql
对MySQL数据库进行简单数据操作python模块主要是:MySQLdb、pymsql,MySQLdb模块主要用于python2.X,而python3.X则使用pymsql,pymysql的使用方法和MySQLdb几乎一样,习惯用MySQLdb的,只需 import MySQLdb 修改为 import pymysql 就可以了
一、安装
pip3 install pymysql
二、基本数据操作
a、增
import pymysql #打开数据库连接
db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="mysql",
port=3306,
charset='utf8',
# 以字典形式展示所查询数据
cursorclass=pymysql.cursors.DictCursor) try:
with db.cursor() as cursor: # 使用cursor()方法获取操作游标
# sql语句
sql = "insert into user(id,username,password) values(4,'liu','1234')"
cursor.execute(sql) # 执行sql语句
#提交
db.commit()
except Exception as e:
print(e)
db.rollback() # 回滚
finally:
db.close() # 关闭连接
b、删
import pymysql #打开数据库连接
db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="mysql",
port=3306,
charset='utf8',
# 以字典形式展示所查询数据
cursorclass=pymysql.cursors.DictCursor) try:
with db.cursor() as cursor: # 使用cursor()方法获取操作游标
# sql语句
sql = "delete from user where id = %d"
cursor.execute(sql %(4)) # 执行sql语句,并向sql语句传递参数
#提交
db.commit()
except Exception as e:
print(e)
db.rollback() # 回滚
finally:
db.close() # 关闭连接
c、查
import pymysql #打开数据库连接
db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="mysql",
port=3306,
charset='utf8',
# 以字典形式展示所查询数据
cursorclass=pymysql.cursors.DictCursor) try:
with db.cursor() as cursor: # 使用cursor()方法获取操作游标
# 查询语句
sql = "select * from user"
cursor.execute(sql) # 执行sql语句
results = cursor.fetchall() # 获取查询的所有记录
print(results)
except Exception as e:
print(e)
finally:
db.close() # 关闭连接
d、改
import pymysql #打开数据库连接
db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="mysql",
port=3306,
charset='utf8',
# 以字典形式展示所查询数据
cursorclass=pymysql.cursors.DictCursor) try:
with db.cursor() as cursor: # 使用cursor()方法获取操作游标
# sql语句
sql = "update user set username = '%s' where id = %d"
cursor.execute(sql % ("hello", 3)) # 执行sql语句, 并传递参数
#提交
db.commit()
except Exception as e:
print(e)
db.rollback() # 回滚
finally:
db.close() # 关闭连接
e、调用自定义函数
import pymysql #打开数据库连接
db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="mysql",
port=3306,
charset='utf8',
# 以字典形式展示所查询数据
cursorclass=pymysql.cursors.DictCursor) try:
with db.cursor() as cursor: # 使用cursor()方法获取操作游标
# 调用自动应函数并传参
cursor.callproc(function_name,args=(data_id,phone,product_id,))
#提交
db.commit()
except Exception as e:
print(e)
db.rollback() # 回滚
finally:
db.close() # 关闭连接
f、 注:
查询数据
# 获取第一行数据
row_1 = cursor.fetchone()
# 获取前n行数据
# row_2 = cursor.fetchmany(3)
# 获取所有数据
# row_3 = cursor.fetchall()
pymysql:源码
【python】-- pymsql 操作MySQL的更多相关文章
- python下操作mysql 之 pymsql
python下操作mysql 之 pymsql pymsql是Python中操作MySQL的模块, 下载安装: pip3 install pymysql 使用操作 1, 执行SQL #!/usr/ ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- (转)Python中操作mysql的pymysql模块详解
原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...
- python数据库操作 - MySQL入门【转】
python数据库操作 - MySQL入门 python学院 2017-02-05 16:22 PyMySQL是Python中操作MySQL的模块,和之前使用的MySQLdb模块基本功能一致,PyMy ...
- day06 python代码操作MySQL
day06 python代码操作MySQL 今日内容 python代码操作MySQL 基于python与MySQL实现用户注册登录 python操作MySQL python 胶水语言.调包侠(贬义词& ...
- python 之操作mysql 数据库实例
对于python操作mysql 数据库,具体的步骤应为: 1. 连接上mysql host 端口号 数据库 账号 密码2. 建立游标3. 执行sql(注意,如果是update,insert,delet ...
- python之操作mysql(一)
使用python操作mysql的思路: 1. 连接数据库:ip,端口号,密码,账号,数据库 2. 建立游标 3.执行sql语句 4.获取执行结果 5.关闭游标,关闭连接 conn = pymysql. ...
- python 安装操作 MySQL 数据库.
以ubuntu和mysql为例 检查自己的机器上面有没有安装数据库 xpower@xpower-CW65S:~$ sudo service mysql start [sudo] xpower 的密码: ...
- Python之 操作 MySQL 数据库
什么是MySQLdb? MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 安装 Pytho ...
随机推荐
- Hadoop之Flume详解
1.日志采集框架Flume 1.1 Flume介绍 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到 ...
- How is javascript asynchronous AND single threaded?
原文: https://www.sohamkamani.com/blog/2016/03/14/wrapping-your-head-around-async-programming/ ------- ...
- smarty在循环的时候计数来显示这是第几次循环的功能
想必有很多人比较喜欢这个smarty循环的时候有个变量增加的功能或比较需要这个功能吧?其实不需要额外的变量,当然你也许根本用不了.我们用smarty内置的就可以了.就是smarty有foreach和s ...
- [Angular] Angular ngSwitch Core Directive In Detail
When want to display different component based on some conditions: <div class='course-category' [ ...
- Oracle基础 触发器
一.触发器 触发器是当特定事件出现时自动执行的代码块.比如,每次对员工表进行增删改的操作时,向日志表中添加一条记录.触发器和存储过程是由区别的:触发器是根据某些条件自动执行的,存储过程是手动条用的. ...
- 打造Android万能上拉下拉刷新框架--XRefreshView(三)
转载请注明出处:http://blog.csdn.net/footballclub/ 打造Android万能上拉下拉刷新框架–XRefreshView(一) 打造Android万能上拉下拉刷新框架–X ...
- R语言初识
# 创建数据集&基本数据管理1.向量 创建函数 c() a <- c(1,2,3,4) a[c(i,j)] :[]给定元素所处位置的数值,即向量a中第i和第j个元素,a[2]第二个元素即 ...
- scrapy框架爬取豆瓣读书(1)
1.scrapy框架 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...
- Redis 的学习和使用
安装Redis 官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.com ...
- 智能提示(一) Solr (suggest)
电商搜索中要实现这么一块功能,当输入文字时候.下拉框提示.类似于百度搜索 在师出名门的基于lucene的solr搜索引擎中.提供了 拼写检查和智能提示这块功能. 拼写检查就是用来检查用户输入的检索 ...