当前位置: 首页 > article >正文

Pymysql之Connection中常用API

Connection中常用API

1、open() :检测数据库是否连接。

connect.open:如果数据库连接返回Trhe,否则返回False。

2、ping(reconnect=True)

connect.ping(reconnect=True):如果reconnect=True表示连接断开后,重新进行连接。

import pymysql.cursors
# 连接数据库
connect = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password='123',
    db='demo_temp',
    charset='utf8',
    cursorclass=pymysql.cursors.DictCursor
)
print(connect.open)  # 打印数据库连接状态
connect.close()  # 关闭数据库连接
connect.ping(reconnect=True)  # 重新连接数据库
print(connect.open)   # 打印数据库连接状态

下面的小动画向我们展示了当connect断开连接后,使用connect.ping(reconnect=True)又重新连接到了数据库。

1562397619622636.gif

3、rollback():回滚当前事务,用法在上面,这里就不再演示了。

4、select_db(db):切换数据库。

# demo_test数据库中users表数据
mysql> select * from users;
+----+-----------+--------+
| id | user      | passwd    |
+----+-----------+--------+
|  1 | 小明      | 123    |
|  2 | 小刚      | 123    |
|  3 | 小红      | 123    |
|  4 | 葫芦娃    | 123     |
|  5 | 小明      | 123    |
+----+-----------+--------+
# demo_temp2库中test表中数据
mysql> select * from test;
+------+------+----------+
| id   | user | password      |
+------+------+----------+
|    1 | abc  | 123       |
+------+------+----------+
import pymysql.cursors
# 连接数据库
connect = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password='123',
    db='demo_temp',
    charset='utf8',
    cursorclass=pymysql.cursors.DictCursor
)
with connect.cursor() as cursor:   # 创建游标
    # 查询demo_temp中users表的数据
    sql = """
        select * from users
        """
    cursor.execute(sql)
    ret = cursor.fetchall()  # 提取查询数据
    print(ret)
    print('-'*80)
    # 切换到demo_temp2数据库,查询test表的数据
    connect.select_db('demo_temp2')
    sql = """
           select * from test
           """
    cursor.execute(sql)
    ret = cursor.fetchall()  # 提取查询数据
    print(ret)
connect.close()   # 关闭数据库连接

打印结果

[{'id': 1, 'user': '小明', 'passwd': '123'}, {'id': 2, 'user': '小刚', 'passwd': '123'}, {'id': 3, 'user': '小红',
'passwd': '123'}, 
{'id': 4, 'user': '葫芦娃', 'passwd': '123'}, {'id': 5, 'user': '小明', 'passwd': '123'}]
--------------------------------------------------------------------------------
[{'id': 1, 'user': 'abc', 'password': '123'}]

5、cursor():创建游标对象,用于操作数据(增、删、改、查)。

6、commit():提交请求,当向数据库中插入数据时,需要使用commit()进行提交,否则数据将不能写入数据库。

7、close():关闭数据库连接。


http://www.kler.cn/news/232581.html

相关文章:

  • 20240206作业
  • 【人工智能】Fine-tuning 微调:解析深度学习中的利器(7)
  • 【Java】eclipse连接MySQL数据库使用笔记(自用)
  • Java面试题2024(Java面试八股文)
  • C语言---计算n的阶乘
  • 云计算运营模式介绍
  • <网络安全>《18 数据安全交换系统》
  • K8S系列文章之 [使用 Alpine 搭建 k3s]
  • 【Flink状态管理(二)各状态初始化入口】状态初始化流程详解与源码剖析
  • 开源大数据集群部署(十)Ranger usersync部署
  • 【RT-DETR有效改进】利用SENetV2重构化网络结构 (ILSVRC冠军得主,全网独家首发)
  • Springboot 整合 Elasticsearch(二):使用HTTP请求来操作ES
  • 开源大型语言模型概览:多语种支持与中文专注
  • ruoyi若依框架SpringSecurity实现分析
  • leetcode (算法)66.加一(python版)
  • 美国服务器如何
  • 眸思MouSi:“听见世界” — 用多模态大模型点亮盲人生活
  • Bee+SpringBoot稳定的Sharding、Mongodb ORM功能(同步 Maven)
  • C#系列-访问SqlServer+Mysql+Oracle数据库(6)
  • 4.0 Zookeeper Java 客户端搭建
  • B2080 计算多项式的值(洛谷)
  • 【Linux】Linux开发工具(yum、gdb、git)详解
  • API接口访问鉴权设计和实现的经验总结
  • 如何开始深度学习,从实践开始
  • seata分布式事务
  • Redis 单线程
  • Qt网络编程-ZMQ的使用
  • 算法随想录第五十一天打卡|309.最佳买卖股票时机含冷冻期, 714.买卖股票的最佳时机含手续费 ,总结
  • npm install express -g报错或一直卡着,亲测可解决
  • HLS 三角函数报错:undefined reference to ‘cordic_apfixed::circ_table_arctan_128‘