pymysql基本操作

作者: 卡拉肖克_潘 | 来源:发表于2020-07-24 18:07 被阅读0次

链接数据库

conn = pymysql.connect(
host=“你的数据库地址”,
user=“用户名”,password=“密码”,
database=“数据库名”,
charset=“utf8”)

connection对象包含的方法

cursor() 使用该连接创建并返回游标
commit() 提交当前事务
rollback() 回滚当前事务
close() 关闭连接

创建游标查询数据

cursor = connection.cursor()
execute("select * from t1") 执行一个数据库的查询命令
fetchone() 取得结果集的下一行
fetchmany(n) 获取结果集的下n行
fetchall() 获取结果集中的所有行
rowcount() 返回数据条数或影响行数
close() 关闭游标对象

execute的字符串拼接

# 将以下代码
sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
res=cursor.execute(sql)
# 可以替换为
sql="select * from userinfo where name=%s and password=%s" #%s需要去掉引号,pymysql会自动加上
res=cursor.execute(sql,[user,pwd])

断线重连。参考:mysql断开重连

def reConnect(self):
    try:
        self.connection.ping()
    except:
        self.connection()

DEMO:参考:PyMySQL的基本使用

增加多条数据

import pymysql
 
conn = pymysql.connect(
    host='192.168.0.103',
    port=3306,
    user='root',
    password='123',
    database='xing',
    charset='utf8'
)
# 获取一个光标
cursor = conn.cursor()
 
# 定义要执行的sql语句
sql = 'insert into userinfo(user,pwd) values(%s,%s);'
data = [
    ('july', '147'),
    ('june', '258'),
    ('marin', '369')
]
# 拼接并执行sql语句
cursor.executemany(sql, data)
 
# 涉及写操作要注意提交
conn.commit()
 
# 获取最新的那一条数据的ID
last_id = cursor.lastrowid
print("最后一条数据的ID是:", last_id)
# 关闭连接
cursor.close()
conn.close()

删除

import pymysql
 
# 建立连接
conn = pymysql.connect(
    host="192.168.0.103",
    port=3306,
    user="root",
    password="123",
    database="xing",
    charset="utf8"
)
# 获取一个光标
cursor = conn.cursor()
# 定义将要执行的SQL语句
sql = "delete from userinfo where user=%s;"
name = "june"
# 拼接并执行SQL语句
cursor.execute(sql, [name])
# 涉及写操作注意要提交
conn.commit()
# 关闭连接
 
cursor.close()
conn.close()

更改数据

import pymysql
 
# 建立连接
conn = pymysql.connect(
    host="192.168.0.103",
    port=3306,
    user="root",
    password="123",
    database="xing",
    charset="utf8"
)
# 获取一个光标
cursor = conn.cursor()
# 定义将要执行的SQL语句
sql = "update userinfo set pwd=%s where user=%s;"
# 拼接并执行SQL语句
cursor.execute(sql, ["july", "july"])
 
# 涉及写操作注意要提交
conn.commit()
 
# 关闭连接
cursor.close ()
conn.close ()

查询数据

import pymysql
 
conn = pymysql.connect (
    host='192.168.0.103',
    port=3306,
    user='root',
    password='123',
    database='xing',
    charset='utf8'
)
# 获取一个光标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 返回字典数据类型
 
# 定义将要执行的sql语句
sql = 'select user,pwd from userinfo;'
# 拼接并执行sql语句
cursor.execute(sql)
 
# 取到查询结果
ret1 = cursor.fetchone()  # 取一条
ret2 = cursor.fetchmany(3)  # 取三条
ret3 = cursor.fetchone()  # 取一条
 
cursor.close()
conn.close()
 
print(ret1)
print(ret2)
print(ret3)  
# 可以获取指定数量的数据
cursor.fetchmany(3)
# 光标按绝对位置移动1
cursor.scroll(1, mode="absolute")
# 光标按照相对位置(当前位置)移动1
cursor.scroll(1, mode="relative")

数据回滚:数据修改后,提交前,将数据回复到修改前。

try:
    # 拼接并执行SQL语句
    cursor.execute(sql1, [user, pwd])
    print(sql1)
    cursor.execute(sql2, [id, hobby])  # 报错的SQL语句
    # 涉及写操作注意要提交
    conn.commit()
except Exception as e:
    print(str(e))
    # 有异常就回滚
    conn.rollback()

相关文章

  • pymysql基本操作

    链接数据库 conn = pymysql.connect(host=“你的数据库地址”,user=“用户名”,pa...

  • 第二十章 Python3 操作 MySQL

    操作 MySQL 一、基本介绍 Python3 操作 MySQL 数据库 可以使用的模块是 pymysql 和 M...

  • pymysql操作mysql数据库

    一、pymysql操作mysql数据库 安装pymysql 1.1 pymysql操作数据库的五行拳 连接数据库使...

  • python操作MySQL

    #### 使用pymysql操作数据库 ##### 1、安装 ``` $ pip install pymysql ...

  • pymysql 操作

    Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数...

  • Python 如何优雅的操作 PyMySQL

    一、PyMysql 在使用Python操作MySQL数据过的过程中,基本的增删改查操作如何更加高效优雅的执行。这里...

  • python3操作sql

    基本介绍 Python3 操作 MySQL 数据库 可以使用的模块是 pymysql 和 MySQLdb。 这个两...

  • 第三十章 Python3 操作 MySQL

    一、基本介绍 Python3 操作 MySQL 数据库 可以使用的模块是 pymysql 和 MySQLdb。 这...

  • 用Python操作MySQL数据库(pymysql库)

    用Python操作MySQL数据库(pymysql库) 一、安装pymysql库 如果你想要使用python操作M...

  • Python3连接数据库

    安装pymysql 先安装pymysql 模块,然后通过pymysql 连接数据库 连接数据库 操作数据库

网友评论

    本文标题:pymysql基本操作

    本文链接:https://www.haomeiwen.com/subject/qntslktx.html