Advertisement
Guest User

code

a guest
Jun 7th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3.  
  4. # connect to the database and return list of tokens
  5. def get_tweets():
  6.     try:
  7.         connection = pymysql.connect(host='localhost',
  8.                                      user='xxx',
  9.                                      password='xxx',
  10.                                      db='xxx',
  11.                                      charset='utf8',
  12.                                      use_unicode=True,
  13.                                      cursorclass=pymysql.cursors.DictCursor)
  14.     except pymysql.err.OperationalError as err:
  15.         print("Operational Error: {0}".format(err))
  16.  
  17.     try:
  18.  
  19.         with connection.cursor() as cursor:
  20.             # read all the tokens
  21.             sql = 'SELECT `text` FROM `tweets`'
  22.             cursor.execute(sql)
  23.             result = cursor.fetchall()
  24.  
  25.             for i in result:
  26.                 print(i['text'])
  27.  
  28.     finally:
  29.         connection.close()
  30.  
  31. get_tweets()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement