【文档学习】pymysql

Installation

pip install pymysql

Connect Object

连接数据库系统

import pymysql
con = pymysql.connect(
    # 主机
    host="182.61.18.124",
    # 端口
    port=3306,
    # 用户名
    user="root",
    # 密码
    password="zhangjiming123.",
    # 编码方式
    charset="utf8",
    # 在数据库连接成功后自动执行的命令
    init_command="show databases;",
    # 设置当前连接数据读取时间超时
    read_timeout = 2,
    # 设置当前连接数据写入时间超时
    write_timeout = 5,
    # 服务器连接超时
    connect_timeout = 10,
    # 设置自动提交模式
    autocommit = True,
    cursorclass = DictCursor
)

打印连接对象

print(con)
# >> <pymysql.connections.Connection object at 0x02D49C70>

服务器信息

# 线程ID
print(con.server_thread_id)
# >> (52689,)
# 容量
print(con.server_capabilities)
# 字符编码
print(con.server_charset)
# 语言
print(con.server_language)
# 公钥
print(con.server_public_key)
# 状态
print(con.server_status)
# 版本号
print(con.server_version)
# >> (52689,)
# >> 3355443199
# >> utf8mb4
# >> 255
# >> None
# >> 2
# >> 8.0.18

主机信息

print(con.get_host_info())
# >> socket 182.61.18.124:3306
print(con.host_info)
# >> socket 182.61.18.124:3306

MySQL版本号

print(con.get_server_info())
# >> socket 182.61.18.124:3306

错误警告

print(con.show_warnings())
# >> None

Cursor Object


发布了82 篇原创文章 · 获赞 468 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/qq_44647926/article/details/105074846