MySQL适配器PyMySQL详解
作者:Blue·Sky 发布时间:2024-01-16 23:49:11
标签:MySQL,适配器,PyMySQL
本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查。
什么是 PyMySQL?
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。
PyMySQL 安装
在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。
PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL。
如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL:
$ pip install PyMySQL
如果你的系统不支持 pip 命令,可以使用以下方式安装:
1、使用 git 命令下载安装包安装(你也可以手动下载):
$ git clone https://github.com/PyMySQL/PyMySQL
$ cd PyMySQL/
$ python3 setup.py install
2、数据库操作实例,直接上代码。
import pymysql
import datainfo
import time
#获取参数
host = datainfo.host
username = datainfo.username
password = datainfo.password
database = datainfo.db
print()
#测试数据库连接
def testconnect():
#打开数据库链接
db = pymysql.connect(host,username,password,database)
#使用cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
#使用execute()方法执行SQL查询
cursor.execute("select version()")
#使用fetchone ()获取单条数据
data = cursor.fetchone()
print(data)
db.close()
#插入数据库
def InsertDate():
#打开数据库链接
db = pymysql.connect(host,username,password,database,charset='utf8')
#使用cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
create_time = time.strftime('%Y-%m-%d %H:%M:%S')
update_time = time.strftime('%Y-%m-%d %H:%M:%S')
start_time = time.strftime('%Y-%m-%d %H:%M:%S')
end_time = time.strftime('%Y-%m-%d %H:%M:%S')
remark = "测试插入信息"
print("开始")
#Sql 插入语句
sql = "insert into demo(start_time,end_time,creat_time,update_time,remark) " \
"VALUES ('%s','%s','%s','%s','%s')"\
%(start_time,end_time,create_time,update_time,remark)
try:
#执行sql
print("执行插入")
tt = cursor.execute(sql)
print(tt)
db.commit()
except UnicodeEncodeError as e :
#发生错误时回滚
print(e)
db.rollback()
db.close()
#查询操作
def selectData():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
sql = "select * from demo where id >='%d'" %(1)
try:
#执行sql
print("执行查询")
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
id = row[0]
start_time = row[1]
end_time = row[2]
create_time = row[3]
update_time = row[4]
remark = row[5]
#打印结果
print("id = %d,start_time=%s,end_time=%s,create_time=%s,update_time=%s,remark=%s" %(id,start_time,end_time,create_time,update_time,remark))
db.commit()
except UnicodeEncodeError as e :
#发生错误时回滚
print(e)
db.close()
#更新操作
def update_data():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
update_time = time.strftime('%Y-%m-%d %H:%M:%S')
sql = "update demo set update_time ='%s' where id >='%d' " %(update_time,1)
try:
#执行sql
print("执行更新")
cursor.execute(sql)
db.commit()
except UnicodeEncodeError as e :
#发生错误时回滚
print(e)
db.rollback()
db.close()
#删除操作
def delete_Date():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
sql = "delete from demo where id <'%d' " %(1)
try:
#执行sql
print("执行删除")
cursor.execute(sql)
db.commit()
except UnicodeEncodeError as e :
#发生错误时回滚
print(e)
db.rollback()
db.close()
if __name__ == '__main__':
testconnect()
InsertDate()
selectData()
update_data()
delete_Date()
来源:http://www.cnblogs.com/BlueSkyyj/p/7559626.html


猜你喜欢
- 我们不可能直接通过node命令来管理远程站点,这样无法保证网站的可持续运行。我们用Forever来解决这个问题,它可以将NodeJS应用以后
- 脚本语言的第一行的目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它。写法:#!/usr/bin/python是告诉操作系统执
- 很多时候,我们都需要获取windows消息提示框的文本内容,例如系统异常信息,软件错误提示等。。。但是如何获取提示信息呢?通常我们的印象中,
- 引言在做科学计算或者模拟仿真的时候,相信不少小伙伴会遇到这样的问题,比如,我们有一个一维数组如下所示:array = [1, 2, 3, 4
- 用了这么长时间,发现自己竟然不知道utf_bin和utf_general_ci这两者到底有什么区别。。ci是 case insensitiv
- 引言 亚马逊网站相较于国内的购物网站,可以直接使用python的最基本的request
- Pytorch torch.distributions库包介绍torch.distributions包包含可参数化的概率分布和采样函数。 这
- Python Assert 为何不尽如人意?Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛
- 最近看JavaScript高级程序设计,大有收获,接下来几天写一下读书笔记。之前写了一篇Ajax初步理解的随笔,里面有个函数用来创建XmlH
- 一、程序功能:为Repeater实现分页二、窗体设计:1、新建ASP.NET Web应用程序,命名为Repeater2,保存路径为http:
- 1。在Asp页面首部<head>加入 Response.Buffer =
- 使用Django服务网页时,只要用户执行导致页面更改的操作,即使该更改仅影响页面的一小部分,它都会将完整的HTML模板传递给浏览器。但是如果
- 我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value。可是有时我们需要对dictionary中
- echarts legend点击事件首先,明确本篇文章的重点,主要有三个:1. 给legend添加点击事件2. 禁用legend点击事件的默
- python中列表的常见操作列表元组的简单操作前面我们已经学过了关于len()函数、赋值运算符及身份运算符的使用,下面简单回顾一下这些在列表
- 使用 filters 实现 英文字母 转大写1、template :<di
- 如果你细心跟踪一下SQL Server数据库服务器的登录过程,你会发现口令计算其实是非常脆弱的,SQL Server数据
- 英文文档:len(s)Return the length (the number of items) of an object. The a
- Python中会遇到很多关于排序的问题,今天小编就带给大家实现插入排序的方法。在Python中插入排序的基本原理类似于摸牌,将摸起来的牌插入
- Form介绍在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来。与此同时我们在好多