Skip to main content

数据库工具

Project description

使用说明

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

# 设置环境变量 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)

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

mjsqltool-2.1.3.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mjsqltool-2.1.3-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file mjsqltool-2.1.3.tar.gz.

File metadata

  • Download URL: mjsqltool-2.1.3.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for mjsqltool-2.1.3.tar.gz
Algorithm Hash digest
SHA256 57dbdea09fc1ae71b4387f6f56b264910a4df3cb0ca0f18f45d920e577b61fcc
MD5 f6e74a6df8d0e8cb1b57cd03a96a861d
BLAKE2b-256 b6d9ec154a50e09f93d672d297f61686d466c3f362b661824c77f891ecbec6b4

See more details on using hashes here.

File details

Details for the file mjsqltool-2.1.3-py3-none-any.whl.

File metadata

  • Download URL: mjsqltool-2.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for mjsqltool-2.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 93a240bd5fb94fe31d575f27caeb8896bb1bb3df02beeef6218096c68b6028b9
MD5 db7090ec65318f4897d03435db380db6
BLAKE2b-256 5d09566e4c0781a9ed8f373d89d610a7f21c107957c585560f2a2d39c0528d5c

See more details on using hashes here.

Supported by

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