pymysql

Posted xumaomao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pymysql相关的知识,希望对你有一定的参考价值。

pymysql基本使用

# 导入pymysql模块
import pymysql
 
# 连接database
conn = pymysql.connect(
    host=“你的数据库地址”, 
    user=“用户名”,password=“密码”,
    database=“数据库名”,
    charset=“utf8”)
 
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
#cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
 
# 定义要执行的SQL语句
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;  #注意:charset=‘utf8‘ 不能写成utf-8
"""
 
# 执行SQL语句
cursor.execute(sql)
 
# 关闭光标对象
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 =insert into userinfo (user,pwd) values (%s,%s);
 
name = wuli
pwd = 123456789
cursor.execute(sql, [name, pwd])
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 = insert into userinfo(user,pwd) values(%s,%s);
data = [
    (july, 147),
    (june, 258),
    (marin, 369)
]
# 拼接并执行sql语句
cursor.executemany(sql, data)
 
# 涉及写操作要注意提交
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 = "insert into userinfo (user, pwd) values (%s, %s);"
name = "wuli"
pwd = "123456789"
# 并执行SQL语句
cursor.execute(sql, [name, pwd])
# 涉及写操作注意要提交
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(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)  

1
2
3
4
5
6
# 可以获取指定数量的数据
cursor.fetchmany(3)
# 光标按绝对位置移动1
cursor.scroll(1, mode="absolute")
# 光标按照相对位置(当前位置)移动1
cursor.scroll(1, mode="relative")

 

数据回滚

import pymysql
 
# 建立连接
conn = pymysql.connect(
    host="192.168.0.103",
    port=3306,
    user="root",
    password="123",
    database="xing",
    charset="utf8"
)
# 获取一个光标
cursor = conn.cursor()
# 定义将要执行的SQL语句
sql1 = "insert into userinfo (user, pwd) values (%s, %s);"
sql2 = "insert into hobby (id, hobby) values (%s,%s);"
user = "july1"
pwd = "july1"
id = "我是错误的id"  #id = "3"
hobby = "打游戏"
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()
 
# 关闭连接
cursor.close()
conn.close()

 

参考资料

https://www.cnblogs.com/xfxing/p/9322199.html

以上是关于pymysql的主要内容,如果未能解决你的问题,请参考以下文章

pymysql安装

django操作mysql

django目录介绍

Python操作MySQL

Python操作数据库

MySQL与python交互