Guest User

Untitled

a guest
Apr 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3. # Connect to the database
  4. connection = pymysql.connect(host='XXX',
  5. user='XXX',
  6. password='XXX',
  7. db='XXX',
  8. charset='utf8mb4',
  9. cursorclass=pymysql.cursors.DictCursor)
  10.  
  11. try:
  12. with connection.cursor() as cursor:
  13.  
  14. print(cursor.execute("SELECT count(*) FROM `table`"))
  15. count = cursor.fetchone()[0]
  16.  
  17. batch_size = 50
  18.  
  19. for offset in xrange(0, count, batch_size):
  20. cursor.execute(
  21. "SELECT * FROM `table` LIMIT %s OFFSET %s",
  22. (batch_size, offset))
  23. for row in cursor:
  24. print(row)
  25. finally:
  26. connection.close()
  27.  
  28. print(cursor.execute("SELECT count(*) FROM `table`"))
  29.  
  30. batch_size = 50
  31.  
  32. connection = pymysql.connect(user='XXX', password='XXX', database='XXX', host='XXX')
  33.  
  34. try:
  35. with connection.cursor() as cursor:
  36. query = "SELECT * FROM `table`"
  37.  
  38. cursor.execute(query)
  39.  
  40. df = pd.read_sql(query, connection)
  41. finally:
  42. connection.close()
Add Comment
Please, Sign In to add comment