Skip to main content

Python连接MySQL的底层核心库

Project description

zdppy_mysql

使用python操作MySQL

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

安装

pip install zdppy_mysql

使用教程

连接MySQL

import zdppy_mysql
from config import host, username, password, database, port

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

添加数据库

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

查询所有表

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

添加表

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

添加数据

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

查询所有数据

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

  • 架构优化

注意事项

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

pip install cryptography

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zdppy_mysql-0.1.5.tar.gz (34.3 kB view details)

Uploaded Source

Built Distribution

zdppy_mysql-0.1.5-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file zdppy_mysql-0.1.5.tar.gz.

File metadata

  • Download URL: zdppy_mysql-0.1.5.tar.gz
  • Upload date:
  • Size: 34.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.19 Darwin/22.6.0

File hashes

Hashes for zdppy_mysql-0.1.5.tar.gz
Algorithm Hash digest
SHA256 12566f82cc6d992b55fcc86bb5baa1cdac84a5b5e701830477b787846152c0d3
MD5 335cbced9e2d98b56c81562dd5aec9a6
BLAKE2b-256 88c03f674b89f17ecca440a60e51f333e7a5b6a9d1e30f25e88bd52deff052f1

See more details on using hashes here.

File details

Details for the file zdppy_mysql-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: zdppy_mysql-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.19 Darwin/22.6.0

File hashes

Hashes for zdppy_mysql-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 da8add51f8cedd59fd385279d049917a91769aa76dbd947741e1c99495fa3389
MD5 1abab198ed72583625aad9e19cea105a
BLAKE2b-256 6415e0e92d2c4657fb0d51473343cb47bf64bf685551462e7c2a7bb2eee67693

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page