SSCursor, SSDictCursor e scroll para conjuntos de dados muito grandes no PyMySQL
This commit is contained in:
@@ -3,12 +3,14 @@
|
||||
# Pypy: https://pypi.org/project/pymysql/
|
||||
# GitHub: https://github.com/PyMySQL/PyMySQL
|
||||
import os
|
||||
from typing import cast
|
||||
|
||||
import dotenv
|
||||
import pymysql
|
||||
import pymysql.cursors
|
||||
|
||||
TABLE_NAME = 'customers'
|
||||
CURRENT_CURSOR = pymysql.cursors.SSDictCursor
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
@@ -18,7 +20,7 @@ connection = pymysql.connect(
|
||||
password=os.environ['MYSQL_PASSWORD'],
|
||||
database=os.environ['MYSQL_DATABASE'],
|
||||
charset='utf8mb4',
|
||||
cursorclass=pymysql.cursors.DictCursor,
|
||||
cursorclass=CURRENT_CURSOR,
|
||||
)
|
||||
|
||||
with connection:
|
||||
@@ -142,18 +144,26 @@ with connection:
|
||||
|
||||
# Editando com UPDATE, WHERE e placeholders no PyMySQL
|
||||
with connection.cursor() as cursor:
|
||||
cursor = cast(CURRENT_CURSOR, cursor)
|
||||
|
||||
sql = (
|
||||
f'UPDATE {TABLE_NAME} '
|
||||
'SET nome=%s, idade=%s '
|
||||
'WHERE id=%s'
|
||||
)
|
||||
cursor.execute(sql, ('Eleonor', 102, 4)) # type: ignore
|
||||
cursor.execute(f'SELECT * FROM {TABLE_NAME} ') # type: ignore
|
||||
cursor.execute(sql, ('Eleonor', 102, 4))
|
||||
cursor.execute(f'SELECT * FROM {TABLE_NAME} ')
|
||||
|
||||
# for row in cursor.fetchall(): # type: ignore
|
||||
# _id, name, age = row
|
||||
# print(_id, name, age)
|
||||
print('For 1: ')
|
||||
for row in cursor.fetchall_unbuffered():
|
||||
print(row)
|
||||
|
||||
for row in cursor.fetchall(): # type: ignore
|
||||
if row['id'] >= 5:
|
||||
break
|
||||
|
||||
print()
|
||||
print('For 2: ')
|
||||
# cursor.scroll(-1)
|
||||
for row in cursor.fetchall_unbuffered():
|
||||
print(row)
|
||||
connection.commit()
|
||||
|
||||
Reference in New Issue
Block a user