码迷,mamicode.com
首页 > 数据库 > 详细

PyMysql

时间:2018-04-26 21:13:49      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:mysql数据库   llb   插入   模块   版本   pymysql   编码   ble   exe   


""""
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

标签:mysql数据库   llb   插入   模块   版本   pymysql   编码   ble   exe   

原文地址:https://www.cnblogs.com/xuerh/p/8954546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!