pymysql基础
2024-08-29 21:10:57
一,基本使用
倒入模块
import pymysql conn=pymysql.connect( host="数据库地址,本机是localhost,别的机器是ip“,
user="用户名,一般是root",
password="密码",
database="数据库名",
charset="utf8" )
得到一个光标对象
cursor=conn.cursor()
定义要执行的语句
sql=“”
执行语句:
cursor.execute(sql)
关闭
cursor。close()
conn.close()
如果想返回的是字典的形式的话
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
二,增删改查
1,增
cursor = conn.cursor()
sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
username = "二狗"
age = 18
# 执行SQL语句
cursor.execute(sql, [username, age])
# 提交事务
conn.commit()
cursor.close()
conn.close()
2,插入数据失败回滚
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
username = "二狗"
age = 18
try:
# 执行SQL语句
cursor.execute(sql, [username, age])
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
3,获取插入数据的ID(关联操作时会用到)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
username = "二狗"
age = 18
try:
# 执行SQL语句
cursor.execute(sql, [username, age])
# 提交事务
conn.commit()
# 提交之后,获取刚插入的数据的ID
last_id = cursor.lastrowid
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
4批量执行
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
data = [("大狗", 18), ("二狗", 20), ("三狗", 21)]
try:
# 批量执行多条插入SQL语句
cursor.executemany(sql, data)
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
5,删
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "DELETE FROM a1 WHERE id=%s;"
try:
cursor.execute(sql, [4])
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
6,改
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 修改数据的SQL语句
sql = "UPDATE a1 SET age=%s WHERE name=%s;"
username = "二狗"
age = 80
try:
# 执行SQL语句
cursor.execute(sql, [age, username])
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
7,查
单条数据
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 查询数据的SQL语句
sql = "SELECT id,name,age from USER1 WHERE id=1;"
# 执行SQL语句
cursor.execute(sql)
# 获取单条查询数据
ret = cursor.fetchone()
cursor.close()
conn.close()
# 打印下查询结果
print(ret)
多条数据
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 查询数据的SQL语句
sql = "SELECT id,name,age from USER1;"
# 执行SQL语句
cursor.execute(sql)
# 获取多条查询数据
ret = cursor.fetchall()
cursor.close()
conn.close()
# 打印下查询结果
print(ret)
三,控制光标
# 可以获取指定数量的数据
cursor.fetchmany(3)
# 光标按绝对位置移动1
cursor.scroll(1, mode="absolute")
# 光标按照相对位置(当前位置)移动1
cursor.scroll(1, mode="relative")
最新文章
- Appium 三种wait方法(appium 学习之改造轮子)
- 小规模的流处理框架.Part 1: thread pools
- RP4412开发板在Android系统编译生成ramdisk-uboot.img
- 2d,3d中旋转推导
- Intel HAXM安装错误处理:(TV-x) is not turned on
- BZOJ3808 : Neerc2012 Labyrinth of the Minotaur
- 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)
- Android之点击切换图片
- IPC:Sockets
- NPOI从数据库中导出数据到Excel
- exists
- Java多线程学习笔记(一)——Thread类中方法介绍
- PL/SQL 自动补全[转]
- 正则替换replace中$1的用法以及常用正则
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
- 编程菜鸟的日记-《软件测试》Ron Patton著-读书笔记
- Windows服务器【由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作】问题调查
- bash 和sed 和gawk
- 详细分析Java中断机制-转载
- Codeforces 1095F Make It Connected 【MST】