이 코드는 mysql 테이블에서 데이터를 가져옵니다. 파이썬의 MySQLdb 모듈을 사용하고 있습니다. SELECT WHERE 조건을 기반으로 각 열의 데이터를 배열 아래에서 검색해야합니다. 예를 들어, 아래 코드에서 위치 필드가 'NY, US'인 모든 데이터를 다른 배열에서 검색하려고합니다. 각 배열은 다른 열 값을 나타냅니다.Python 가져 오기 MySQL에서 배열로 데이터 열
import numpy
import MySQLdb
db = MySQLdb.connect("localhost", "root", "", "test")
cursor = db.cursor()
sql = "SELECT * FROM usa_new WHERE location = 'NY, US'"
try:
cursor.execute(sql)
results = cursor.fetchall()
discresults = {}
for row in results:
id = row[0]
location = row[1]
temp_f = row[2]
pressure_mb = row[3]
wind_dir = row[4]
wind_mph = row[5]
relative_humidity = row[6]
timestamp = row[7]
except:
print "Error: unable to fecth data"
db.close()
문제가 있습니까?
거기에 'discresults {}'란 무엇입니까? – noel
커밋() 메서드를 사용하여 쿼리를 완료해야한다고 생각합니다. – noel
@shakabra acutallyy 나는이 코드를 조금 편집했다. 이전에는 각 열에서 단일 값을 꺼내고 있었다. 그래서 discresults가 여기에 있습니다. – khan