0
我想通过JOIN执行SQL查询,我可以将表别名传播到生成的字典键中。例如,我可以有一个查询sqlite3:在JOIN查询中保留表名
query = """
SELECT t1.col1,t2.col1,t2.col2
FROM table1 t1 JOIN table2 t2
ON t1.col0=t2.col0
"""
,我想输出来维持T1,T2的别名,因为我有重复的列名(COL1)。我会运行
con = sqlite3.connect(dbpath, isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES)
def dict_factory(cursor, row):
d = {}
for idx,col in enumerate(cursor.description): d[col[0]] = row[idx]
return d
db.dict = con.cursor()
db.dict.row_factory = dict_factory
result = db.dict.execute(query).fetchone()
但是这会覆盖col1
的值。我怎么能回报,说,
{'t1.col1':123, 't2.col1':234, 't2.col2':345}
谢谢!