游标上的mysql-connector-python循环非常慢

时间:2016-02-05 18:18:58

标签: python mysql mysql-connector-python

我无法使用mysql-connector-python(python版本3.5)查询mysql数据库。我试图为整个站点列表提取特定日期时间范围的air_temperature。第一个stn的第一个查询工作正常,但第二个查询只是永远挂起。

import mysql.connector
import datetime

connection = mysql.connector.connect(host=' ', 
                       user=' ', 
                       passwd=' ', 
                       database=' ', 
                       port= ) 

stn_id = [‘stn_01’,’stn_02’, ’stn_03’, ’stn_04’, ’stn_05’]
datetime_start = datetime.datetime(2016, 1, 1, 00, 00, 00)
datetime_end   = datetime.datetime(2016, 2, 1, 00, 00, 00)

for stn in range(0,n_stn,1): 
    cursor = connection.cursor(buffered=True)

    q = """
            SELECT time_stamp, air_temperature
            FROM %s
            WHERE time_stamp >= %s
            AND   time_stamp <= %s
        """

    cursor.execute(q,(stn_id[stn], datetime_start, datetime_end))
    temp_results = cursor.fetchall()
    # do something with the results 
    cursor.close() 

0 个答案:

没有答案