Python使用zdppy_mysql操作MySQL和MariaDB数据库快速入门教程

zdppy_mysql

使用python操作MySQL

项目开源地址:https://github.com/zhangdapeng520/zdppy_mysql

安装

bash 复制代码
pip install zdppy_mysql

使用教程

连接MySQL

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
)
print(db)

添加数据库

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
)

# 使用游标对象执行SQL语句
with db.cursor() as cur:
    # 创建数据库
    sql = "create database if not exists test"
    cur.execute(sql)

    # 查询所有数据库
    sql = "show databases"
    cur.execute(sql)
    print(cur.fetchall())

查询所有表

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
    cursorclass=zdppy_mysql.cursors.DictCursor,
)

# 使用游标对象执行SQL语句
with db.cursor() as cur:
    # 查询所有数据库
    sql = "show tables"
    cur.execute(sql)
    print(cur.fetchall())

添加表

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
    cursorclass=zdppy_mysql.cursors.DictCursor,
)

# 使用游标对象执行SQL语句
with db.cursor() as cur:
    # 查询所有数据库
    sql = "create table user(id int primary key auto_increment, name varchar(255))"
    cur.execute(sql)
    db.commit()

添加数据

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
    cursorclass=zdppy_mysql.cursors.DictCursor,
)

# 使用游标对象执行SQL语句
with db.cursor() as cur:
    cur.execute("insert into user(name) values(%s)", ("张三1",))
    cur.execute("insert into user(name) values(%s)", ("张三2",))
    cur.execute("insert into user(name) values(%s)", ("张三3",))

    # 必须加commit才会提交到数据库保存
    db.commit()

查询所有数据

python 复制代码
import zdppy_mysql
from config import host, username, password, database, port

# 连接数据库
db = zdppy_mysql.connect(
    host,
    username,
    password,
    database,
    port,
    cursorclass=zdppy_mysql.cursors.DictCursor,
)

# 使用游标对象执行SQL语句
with db.cursor() as cur:
    # 查询所有数据库
    cur.execute("select * from user")
    print(cur.fetchall())

版本历史

  • 0.1.1 解决mysql8初次连接报auth异常的问题
  • 0.1.4 移除Database类及其他语法糖,全部移交mcrud处理

v0.1.5

  • 架构优化

注意事项

如果报权限异常错误,请手动执行依赖:

bash 复制代码
pip install cryptography
相关推荐
飞翔沫沫情14 分钟前
《MySQL 5.7.44审计合规实践:插件集成与日志分割自动化方案》
数据库·mysql·mysql审计
MXsoft61814 分钟前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库
乌旭44 分钟前
AI芯片混战:GPU vs TPU vs NPU的算力与能效博弈
人工智能·pytorch·python·深度学习·机器学习·ai·ai编程
不辉放弃1 小时前
SQL 主键(Primary Key)
数据库·sql·oracle
MinggeQingchun1 小时前
Python - 爬虫-网页抓取数据-库requests
爬虫·python·requests
qq_339282231 小时前
PostgreSQL-常用命令
数据库·postgresql·oracle
拓端研究室TRL2 小时前
Python贝叶斯回归、强化学习分析医疗健康数据拟合截断删失数据与参数估计3实例
开发语言·人工智能·python·数据挖掘·回归
沸材2 小时前
Redis——实现消息队列
数据库·redis·消息队列
しかし1181142 小时前
C语言队列的实现
c语言·开发语言·数据结构·数据库·经验分享·链表
⁤⁢初遇2 小时前
MySQL---数据库基础
数据库