解决pandas使用sqlalchemy保存到Mysql数据库时,bool布尔类型数据转为tinyint数据的读取

pandas在使用to_sql()保存数据到数据表中,Mysql会将bool类型的数据转为tinyint类型,比如:
在这里插入图片描述

此时数据表字段的类型为:
在这里插入图片描述

读取的时候,如果直接使用read_sql会原封不动的读取成1或0的数据,因此我们存储的时候没有办法将true/false存储进数据库,只能在读取的时候使用:

data_df = pd.read_sql_query(
	text('select * from test_bool_data'), con=engine.connect(), dtype={
		"bool_true": bool, "bool_false": bool,
	})  # 读取sql

这样就可以在读取出数据之后,将指定的这两列bool_truebool_false转为bool布尔类型的数据

示例程序

import pandas as pd
import numpy as np
from sqlalchemy import create_engine, text


def get_engine():
    mysql_config = {
        "db": "just_test",
        "host": "127.0.0.1",
        "user": "XXXX",
        "password": "XXXX",
        "port": 3306,
    }
    engine = create_engine(
        "mysql+pymysql://{}:{}@{}:{}/{}".format(mysql_config['user'], mysql_config['password'], mysql_config['host'],
                                                mysql_config['port'], mysql_config['db']))
    return engine


def save_sql():
    engine = get_engine()
    # 保存dataframe
    tmp_df = pd.DataFrame(np.random.random(size=(10, 5)))
    tmp_df['bool_true'] = True
    tmp_df['bool_false'] = False
    tmp_df.to_sql('test_bool_data', con=engine, if_exists='append', index=False)


def read_sql():
    engine = get_engine()
    data_df = pd.read_sql_query(text('select * from test_bool_data'), con=engine.connect(),
                                dtype={
                                    "bool_true": bool,
                                    "bool_false": bool,
                                })  # 读取sql
    print(data_df.head())


if __name__ == '__main__':
    read_sql()

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 20:38:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 20:38:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 20:38:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 20:38:05       18 阅读

热门阅读

  1. 将PostgreSQL插件移植到openGauss指导

    2024-03-15 20:38:05       18 阅读
  2. 【TypeScript】快速掌握TypeScript的基本语法

    2024-03-15 20:38:05       19 阅读
  3. 2024年集创赛FPGA紫光同创赛道男女声,童声变声

    2024-03-15 20:38:05       18 阅读
  4. 蓝桥杯刷题--python-21

    2024-03-15 20:38:05       18 阅读
  5. python中什么是装饰器

    2024-03-15 20:38:05       16 阅读
  6. 在Ubuntu中如何基于conda安装jupyterlab

    2024-03-15 20:38:05       20 阅读
  7. 国军标GJB150A霉菌试验解读

    2024-03-15 20:38:05       19 阅读
  8. Go json Marshal & UnMarshal 的一点小 trick

    2024-03-15 20:38:05       20 阅读