PyMysql

Posted xuerh

tags:

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


""""
Pymysql介绍
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
Django中也可以使用PyMySQL连接MySQL数据库。

基本使用
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,
database=“数据库名”,charset=“utf8”)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的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;

# 执行SQL语句
cursor.execute(sql)
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()

返回字典格式的数据
conn = pymysql.connect(
host="127.0.0.1", port=3306, user="root",
password="123456", database="task", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select * from goods;")
ret2 = cursor.fetchall()
print("=" * 100)
cursor.close()
conn.close()
print(ret2)

注意:
charset=“utf8”,编码不要写成"utf-8"


二 . 增删改查 操作

1 . 增 加 插入数据失败回滚
conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", password="123456",database="task",charset="utf8")
cursor = conn.cursor()
sql = "insert into tb2(name, age) values(%s, %s);"

name = "小刚"
age = 22
try:
cursor.execute(sql, [name, age])
conn.commit()
except Exception as e:
conn.rollback()
print(e)
cursor.close()
conn.close()



批量操作 (注意是cursor.executemany)

conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", password="123456",database="task",charset="utf8")
cursor = conn.cursor()
sql = "insert into tb2(name, age) values(%s, %s);"
data = [
("ahuango",6),
("ahoongi",7),
]
try:
cursor.executemany(sql, data)
conn.commit()
except Exception as e:
conn.rollback()
print(e)
cursor.close()
conn.close()


获取插入数据的ID(关联操作时会用到)

conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", password="123456",database="task",charset="utf8")
cursor = conn.cursor()
sql = "insert into tb2(name, age) values(%s, %s);"

name = "小刚2"
age = 20
try:
cursor.execute(sql, [name, age])
conn.commit()

# 获取刚插入数据的 id
last_id = cursor.lastrowid
print(last_id)
except Exception as e:
conn.rollback()
print(e)
cursor.close()
conn.close()


删除数据
conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", password="123456",database="task",charset="utf8")
cursor = conn.cursor()
sql = "delete from tb2 where id=%s;"

try:
cursor.executemany(sql, [10])
conn.commit()
except Exception as e:
conn.rollback()
print(e)
cursor.close()
conn.close()



修改数据
conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", password="123456",database="task",charset="utf8")
cursor = conn.cursor()
sql = "update tb2 set age=%s where id=%s;"

age = 88
id = 12
try:
cursor.execute(sql, [age, id])
conn.commit()
except Exception as e:
conn.rollback()
print(e)
cursor.close()
conn.close()


最重要的查看数据
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", database="task", charset="utf8")
cursor = conn.cursor()
sql = "select * from tb2;"

cursor.execute(sql)
ret1 = cursor.fetchone() #取一条
ret2 = cursor.fetchall() #取全部的数据
cursor.close()
conn.close()
print(ret1)
print(ret2) #返回元组 形式


进阶用法

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































































































































































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

pymysql安装

django操作mysql

django目录介绍

Python操作MySQL

Python操作数据库

MySQL与python交互