import mysql.connector as connector
Делаю так:
def select_computed_series_ids(self): connection = None cursor = None try: connection = connector.connect( host=self.__host, database=self.__database, password=self.__password, user=self.__user) cursor = connection.cursor() cursor.execute(""" SELECT series_id FROM series AS WHERE status <> 'X' -- LIMIT 5 """) return {item[0] for item in cursor} finally: if cursor: cursor.close() if connection: connection.close()
Пока решил проблему так:
def select_computed_series_ids(self): connection = None cursor = None try: connection = connector.connect( host=self.__host, database=self.__database, password=self.__password, user=self.__user) cursor = connection.cursor(prepared=True) query = """ SELECT series_id FROM series WHERE series_id > %s AND status <> 'X' ORDER BY series_id LIMIT 1000 """ last_series_id = 0 ids = [] while True: cursor.execute(query, (last_series_id,)) row = cursor.fetchone() if row is None: break while row is not None: last_series_id = row[0] ids.append(last_series_id) row = cursor.fetchone() return set(ids) finally: if cursor: cursor.close() if connection: connection.close()
Но остаётся вопрос, как избежать зависание программы на работе курсора, т.е. правильно ли у меня делается выборка данных и какой максимум записей курсор может выдать?