# Connect to the database connection = pymysql.connect(host='localhost', user='user', password='passwd', db='db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try: with connection.cursor() as cursor: # Read a single record sql = "SELECT `id`, `password` FROM `users`" cursor.execute(sql) result = cursor.fetchall() print(result) finally: connection.close()
class SSCursor(Cursor): """ Unbuffered Cursor, mainly useful for queries that return a lot of data, or for connections to remote servers over a slow network.
Instead of copying every row of data into a buffer, this will fetch rows as needed. The upside of this, is the client uses much less memory, and rows are returned much faster when traveling over a slow network, or if the result set is very big.
There are limitations, though. The MySQL protocol doesn't support returning the total number of rows, so the only way to tell how many rows there are is to iterate over every row returned. Also, it currently isn't possible to scroll backwards, as only the current row is held in memory. """
def fetchall_unbuffered(self): """ Fetch all, implemented as a generator, which isn't to standard, however, it doesn't make sense to return everything in a list, as that would use ridiculous memory for large result sets. """ return iter(self.fetchone, None)
with src_pc_database.cursor() as src_cursor: sql = "select * from user" src_cursor.execute(sql) result = src_cursor.fetchone() while result is not None: result = src_cursor.fetchone()