首页 > 解决方案 > 有没有办法在 python var 中保存 mysql 列值?

问题描述

我正在尝试将列值保存到 python 变量中;根据从 XML 获得的值,我在名为 id_mType 和 id_meter 的列中查询我的数据库中的 int。为了测试它,我做了下一个(我是使用数据库的新手):

m = 'R1'
id_cont1 = 'LGZ0019800712'
xdb = cursor.execute("SELECT id_mType FROM mType WHERE m_symbol = %s", m)
xdb1 = cursor.execute("select id_meter from meter where nombre = %s", 
id_cont1)

print (xdb)
print (xdb1)

每次我得到值“1”,其中 id_mType 为 'R1' = 3 和 id_meter= 7 为 id_cont1 值。我需要将其插入另一个表(其中有 FK:id_meter 和 id_mType。不知道是否有最简单的方法)

标签: pythonmysqlpymysql

解决方案


您可以将其存储在列表中。可以吗?

results=cursor.fetchall()
my_list=[]
for result in results:
    my_list.append(result[0])

现在my_list应该保存SQL您的查询返回的列。


推荐阅读