pymysql 处理pymysql取值带有Decimal和datetime.datatime的数据


import pymysql
from loguru import logger

from pymysql import converters, FIELD_TYPE

conv = converters.conversions
conv[FIELD_TYPE.NEWDECIMAL] = float  # convert decimals to float
conv[FIELD_TYPE.DATE] = str  # convert dates to strings
conv[FIELD_TYPE.TIMESTAMP] = str  # convert dates to strings
conv[FIELD_TYPE.DATETIME] = str  # convert dates to strings
conv[FIELD_TYPE.TIME] = str  # convert dates to strings

def client_database(sql):
    # 打开数据库连接
    db = pymysql.connect(host="127.0.0.1", user="root", password="1111", db="data_center",
                         port=3306, conv=conv)
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 使用 execute()  方法执行 SQL 查询
    cursor.execute(sql)
    # 使用 fetchall() 方法获取s所有数据.
    datas = cursor.fetchall()
    db.close()
    return list(datas)

posted @ 2022-09-24 01:03  darling331  阅读(352)  评论(0编辑  收藏  举报