一,基本使用

倒入模块

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

最新文章

  1. Appium 三种wait方法(appium 学习之改造轮子)
  2. 小规模的流处理框架.Part 1: thread pools
  3. RP4412开发板在Android系统编译生成ramdisk-uboot.img
  4. 2d,3d中旋转推导
  5. Intel HAXM安装错误处理:(TV-x) is not turned on
  6. BZOJ3808 : Neerc2012 Labyrinth of the Minotaur
  7. 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)
  8. Android之点击切换图片
  9. IPC:Sockets
  10. NPOI从数据库中导出数据到Excel
  11. exists
  12. Java多线程学习笔记(一)——Thread类中方法介绍
  13. PL/SQL 自动补全[转]
  14. 正则替换replace中$1的用法以及常用正则
  15. 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
  16. 编程菜鸟的日记-《软件测试》Ron Patton著-读书笔记
  17. Windows服务器【由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作】问题调查
  18. bash 和sed 和gawk
  19. 详细分析Java中断机制-转载
  20. Codeforces 1095F Make It Connected 【MST】

热门文章

  1. 机器学习基本概念:batch_size、epoch、 iteration
  2. HTTP系列之:HTTP缓存
  3. PS-头发丝抠图
  4. 重启网络服务 network 出现问题
  5. Pulsar の 保证消息的顺序性、幂等性和可靠性
  6. https(ssl) 免费证书
  7. JVM双亲委派模型及其优点
  8. WebService学习总结(四)--基于CXF的服务端开发
  9. Linux - 解决使用 apt-get 安装 yum 的时耗报 E: Unable to locate package yum 的错误
  10. Java学习笔记--常用容器