python 设置崖山数据库查询返回的数据类型为字典

方案

复制代码
import yasdb


class YASDBUtil:

def __init__(self, host, user, password, port=1688):
self.connect = yasdb.connect(
host=host,
port=port,
user=user,
password=password
)

def select(self, sql):
cursor = self.connect.cursor()
cursor.execute(sql)

#获取列名
columns = [column[0] for column in cursor.description]
data = cursor.fetchall()

# 将结果转换为字典列表
dict_results = [dict(zip(columns, row)) for row in data]
self.connect.commit() # 提交事务,如果不提交,那么下次查询,查不到最新的数据
cursor.close()
return dict_results
复制代码

测试结果

D:\code\apiobjectframework\venv\Scripts\python.exe D:\code\apiobjectframework\common\yasdb_util.py 
{'USER_ID': 1881166937720496130, 'CREATED_BY': 1744650000571617281, 'CREATED_TIME': datetime.datetime(2025, 1, 20, 10, 28, 43, 3000), 'UPDATED_BY': 1744650000571617281, 'UPDATED_TIME': datetime.datetime(2025, 2, 11, 15, 1, 34, 322000), 'DEPT_ID': 2420008010000000003, 'USER_CODE': '80105517441', 'USER_NAME': '呼思佳', 'USER_CATEGORY_ID': 2, 'USER_STATUS_ID': 1, 'WATER_USER_ADDRESS': '盛世御景小区 80室', 'HOUSE_NUMBER': '23', 'OPEN_DATE': datetime.datetime(2023, 1, 20, 10, 28, 43, 3000),}

进程已结束,退出代码为 0
posted @   未来可期_Durant  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示