Python 3.x 使用python pyodbc返回迭代器
我正试图用python编写一个DataAccessLayer。 其中一个函数应该为一大组行返回一个迭代器(我不想获取所有行并占用大量内存) 迭代器将在另一个类中使用,该类使用我的DAL类为每个用户检查某些内容 我知道我不应该返回游标本身,但是如何给另一个类一个迭代器呢 我的朋友:Python 3.x 使用python pyodbc返回迭代器,python-3.x,pyodbc,Python 3.x,Pyodbc,我正试图用python编写一个DataAccessLayer。 其中一个函数应该为一大组行返回一个迭代器(我不想获取所有行并占用大量内存) 迭代器将在另一个类中使用,该类使用我的DAL类为每个用户检查某些内容 我知道我不应该返回游标本身,但是如何给另一个类一个迭代器呢 我的朋友: import src.data_handler.data_handler_conf as conf import time import pyodbc class DataHandler: def __init_
import src.data_handler.data_handler_conf as conf
import time
import pyodbc
class DataHandler:
def __init__(self):
# Get configuration
self.db_url = conf.DB_URL
self.db_name = conf.DB_NAME
self.db_username = conf.DB_USERNMAE
self.db_password = conf.DB_PASSWORD
self.db_conn = None
self.connect()
def connect(self):
self.db_conn = pyodbc.connect("DRIVER={FreeTDS}"
";SERVER=" + self.db_url +
";DATABASE=" + self.db_name +
";UID=" + self.db_username +
";PWD=" + self.db_password)
self.db_conn.setencoding(encoding='utf-8')
def check_users(self):
cursor = self.db_conn.cursor()
cursor.execute("select user_name, field1 from users")
# What should i return?!
return cursor
我想你应该能够使用发电机,比如:
def get_结果(cnxn):
crsr=cnxn.cursor()
crsr.execute(“从百万行中选择*)
row=crsr.fetchone()
而行:
产量行
row=crsr.fetchone()
它将返回一个序列