Zugriff auf einen Wert aus MySQL mit pymysql in python

Ich versuche, Holen eines Wertes aus einer Datenbank mit pymysql. Meine aktuelle Lösung ist 3 Zeilen code. Ist dies der beste Weg, dies zu behandeln?

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]

Benötige ich diese for-Schleife, um den Zugriff auf den Inhalt cur.fetchall()?

Ich bin mit Python 3.2.3

InformationsquelleAutor posop | 2012-08-23
Schreibe einen Kommentar