Skip to main content

使用说明

变量环境设置(数据库的链接信息存储在变量环境中)

# 设置环境变量 user 是用户名,password 是密码,192.168.1.10:22484 数据库地址
os.environ['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://user:password@192.168.1.10:22484/'

# 获取环境变量,如果不存在则使用默认值
value = os.environ.get('SQLALCHEMY_DATABASE_URI', 'default_value')
print(value+"DY_DSLP")

windwow cmd 设置永久环境变量

setx SQLALCHEMY_DATABASE_URI "mysql+pymysql://user:password@192.168.1.10:22484/"

1、安装包模块

pip install mjsqltool
pip install --upgrade mjsqltool

2、引入包模块

from mjsqltool import SqlConnect,DataConvert,SqlExecute

SqlConnect

链接数据库,使用完毕后自动关闭

# 数据库名称
db  = 'database'
with SqlConnect(db) as cn:
    .....

DataConvert

数据转换 1、清洗数据 2、数据转SQL

convert = DataConvert()
df = pd.read_exce("jjjj.xlsx")

# 清洗数据
df = convert.convert_to_cleandata(df)

# 转成SQL语句  sql_type:   保存类型 有两个可选参数 'INSERT' 和 'REPLACE'
data = df
table_name = '测试表格'
sql_type='REPLACE'
query = convert.convert_to_sqlstring(data,table_name, sql_type)

SqlExecute

SqlExecute 将数据保存到MySQL SqlExecute 继承了 DataConvert,

from mjsqltool import SqlConnect,DataConvert,SqlExecute
import pandas as pd

with SqlConnect("MJ_DATA") as cn:
    df = pd.read_excel(r"C:\Users\manji\Downloads\联盟订单.xlsx")
    SqlExecute().data_cleanerSave_tosql(df,"测试表格",cn)

SqlExecute().data_cleanerSave_tosql

参数说明

sql_type='INSERT', 
# sql_type='INSERT' 自动忽略重复主键记录 有两个可选参数 'INSERT' 和 'REPLACE'

datalong = None, 
# datalong="only" 为 only时,只写入mysql中存在的数据字段

chunk_size=10000   
# 每次最大写入数据的数据记录,例如90条数据,每次写入10条,则自动拆分成10次写入

使用pandas读取MySQL数据

reportSql = 'select * from TB_table'
with SqlConnect(database="TB_GGTF") as cn:
    reportSql = text(reportSql)  # 将字符串转换为可执行的 SQL 对象
    df = pd.read_sql_query(reportSql,cn)  
    print(df)

打包

python setup.py sdist bdist_wheel

pgsql

测试代码

from mjsqltool import ConnectDB # 连接pgsql数据库
from mjsqltool import cleandf  # 清洗df数据 
from mjsqltool import upsert_factory     # 主键冲突处理方法
from mjsqltool import dftopostgresql     # 数据写入到pgsql数据库(包含清洗数据和处理主键冲突)

import pandas as pd


if __name__ == '__main__':
    df = pd.read_excel(r"C:\Users\manji\Downloads\服务费结算单3月.xlsx")
    DB_URI='postgresql+psycopg2://postgres:manji1688@127.0.0.1:5432/'
    with ConnectDB(database='shop_data', db_uri=DB_URI) as db:
        result = dftopostgresql(df, db, 'douyin_shop',table_name='dy_dp_gyl_fwfjsd_copy1',on_conflict='do_update', chunksize=10)
        print(result)

Metadata

Release files for mjsqltool 2.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mjsqltool 2.1.3
File Size Uploaded Provenance Actions
mjsqltool-2.1.3.tar.gz 14.5 kB Without provenance Details for mjsqltool-2.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mjsqltool 2.1.3
File Interpreter ABI Platform Actions
mjsqltool-2.1.3-py3-none-any.whl Python 3 none any Details for mjsqltool-2.1.3-py3-none-any.whl

Total release size: 30.7 kB

Release files / mjsqltool-2.1.3.tar.gz

Download URL mjsqltool-2.1.3.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
57dbdea09fc1ae71b4387f6f56b264910a4df3cb0ca0f18f45d920e577b61fcc
BLAKE2b-256 checksum
How to use checksums
b6d9ec154a50e09f93d672d297f61686d466c3f362b661824c77f891ecbec6b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / mjsqltool-2.1.3-py3-none-any.whl

Download URL mjsqltool-2.1.3-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93a240bd5fb94fe31d575f27caeb8896bb1bb3df02beeef6218096c68b6028b9
BLAKE2b-256 checksum
How to use checksums
5d09566e4c0781a9ed8f373d89d610a7f21c107957c585560f2a2d39c0528d5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for mjsqltool 2.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page