Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3.  
  4. # Connect to the database
  5. connection = pymysql.connect(host='localhost',
  6. user='root',
  7. password='',
  8. db='wanqu',
  9. charset='utf8',
  10. cursorclass=pymysql.cursors.DictCursor)
  11.  
  12. try:
  13. with connection.cursor() as cursor:
  14. # Read a single record
  15. sql = "SELECT `id`, `creation_date` FROM `post`"
  16. cursor.execute(sql)
  17. result = cursor.fetchall()
  18.  
  19. for each in result:
  20. print(each.get('creation_date'))
  21. id = each.get('id')
  22. cd = each.get('creation_date')
  23. if len(cd) > 10:
  24. sql_new = "UPDATE `post` SET `creation_date` = %s WHERE `id` = %s"
  25. print(cd[:10])
  26. cursor.execute(sql_new, (cd[:10], id))
  27. connection.commit()
  28.  
  29. with connection.cursor() as cursor:
  30. sql = "SELECT `id`, `creation_date` FROM `issue`"
  31. cursor.execute(sql)
  32. result = cursor.fetchall()
  33.  
  34. for each in result:
  35. print(each.get('creation_date'))
  36. id = each.get('id')
  37. cd = each.get('creation_date')
  38. if len(cd) > 10:
  39. sql_new = "UPDATE `issue` SET `creation_date` = %s WHERE `id` = %s"
  40. print(cd[:10])
  41. cursor.execute(sql_new, (cd[:10], id))
  42. connection.commit()
  43. finally:
  44. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement