pos*_*sop 1 python mysql mysql-python python-3.x
我试图从数据库中获取一个值pymysql.我目前的解决方案是3行代码.这是处理这个问题的最佳方法吗?
import pymysql
conn = pymysql.connect(host='localhost', user='root', passwd='')
conn.autocommit(True)
cur = conn.cursor()
memory = '100'
# I want to access one item from mySQL
cur.execute("""SELECT id FROM scada.sensors WHERE memory='{}'""".format(memory))
for x in cur.fetchall(): # only runs once
sensor_id=x[0]
Run Code Online (Sandbox Code Playgroud)
我需要这个for循环来访问内容cur.fetchall()吗?
我使用的是Python 3.2.3