python - 有没有办法在 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。不知道是否有最简单的方法)
解决方案
您可以将其存储在列表中。可以吗?
results=cursor.fetchall()
my_list=[]
for result in results:
my_list.append(result[0])
现在my_list
应该保存SQL
您的查询返回的列。
推荐阅读
- c++ - 如果没有所有权,Boost::thread::mutex 可以解锁互斥锁吗?
- android - android.widget.Editor 崩溃。android.content.res.Resources$NotFoundException
- python - 使用列表中的变量或项目作为代码的一部分(而不是字符串或变量)或变量本身?
- javascript - 在中间表中序列化多对多关系和一对多
- java - 如何使用 Asynctask 将 Json 对象作为值发布?
- php - 只打印数组的值
- vue.js - Veevalidate 总是返回 true Vuejs
- javascript - 管理 javascript 依赖项
- prolog - Prolog问题,操作列表列表
- javascript - Angular.js:为一些特殊字符创建正则表达式