6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fetchmany で fetch する

Last updated at Posted at 2014-09-19

時々使うので自分のメモ的に。

定義

def fetchmany(cursor, size=1000):
    """
    1000件づつ fetch する
    """
    while True:
        rows = cursor.fetchmany(size)
        if not rows:
            break
        for row in rows:
            yield row

利用

# cursor を取得
import MySQLdb
conn = MySQLdb.connect()
conn.cursorclass = MySQLdb.cursors.DictCursor  # select した結果を dict で取りたい時等
cursor = conn.cursor()

# 検索
cursor.execute('SELECT foo FROM bar WHERE baz = %s', ('piyo', ))
for row in fetchmany(cursor):
    print(row['foo'])

参考

僕が所属している会社社員募集中みたいです。なんか、 Python 書いてみてーなと思った人は応募してみてくださいな。 (すてま)

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?