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