【问题标题】:Get data from select statement mysql.connector从 select 语句 mysql.connector 中获取数据
【发布时间】:2020-07-12 20:35:57
【问题描述】:

如何将select语句的内容存储到变量中?

我有一个名为 testing 的表,其中包含 2 个字段:ID 和 events。

我使用SELECT * FROM testing WHERE id=1; 来获取行,但是如何获取该行的事件列中的数据并将其存储到变量x 中?我在网上找不到如何做到这一点。

【问题讨论】:

  • x = testing%rowtype ?这是做什么的?

标签: python sql mysql-connector


【解决方案1】:

我想您想在 python 中执行此操作,并且我想您已经连接了数据库并将其放入名为 conn 的变量中

sql = "select * from Laptop"
cursor = conn.cursor()
cursor.execute(sql)
x = cursor.fetchall()

您的结果将存储在x 变量中

【讨论】:

  • x["events"]会返回返回结果的events列的数据吗?
  • @mistertwst x 有一个元组数组,如果你想得到一个特定的行,你需要通过 index 得到它。这是具有 3 列的表的 x 变量示例:x = [ (1,'LENOVO' , 30000) , (2,'HP', 45 000) ]
  • 所以如果我想获得“LENOVO”,我会选择y = x[0][1]?
  • @mistertwst 是的
  • 感谢您的帮助!
猜你喜欢
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
相关资源
最近更新 更多