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
知道、想到、做到、得到

浙公网安备 33010602011771号