Created
July 8, 2016 06:33
-
-
Save justnoise/d8720e566c841a2bcbd12c7744de6dcc to your computer and use it in GitHub Desktop.
Simple demonstration of streaming queries from mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import MySQLdb | |
| import logging | |
| log = logging.getLogger(__name__) | |
| class StreamingMysqlClient(object): | |
| def __init__(self, host, user, password, dbname): | |
| self.conn = MySQLdb.connect(host, user, password, dbname) | |
| def stream_query(self, query): | |
| cursor = self.conn.cursor(MySQLdb.cursors.SSCursor) | |
| cursor.execute(query) | |
| row = cursor.fetchone() | |
| while row is not None: | |
| yield row | |
| row = cursor.fetchone() | |
| def main(): | |
| db = StreamingMysqlClient('localhost', 'justnoise', 'ILikeToParty', 'party_times') | |
| results = db.stream_query('select * from shindigs') | |
| for values in results: | |
| print values | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment