Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- # Connect to the database
- connection = pymysql.connect(host='localhost',
- user='root',
- password='',
- db='wanqu',
- charset='utf8',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- # Read a single record
- sql = "SELECT `id`, `creation_date` FROM `post`"
- cursor.execute(sql)
- result = cursor.fetchall()
- for each in result:
- print(each.get('creation_date'))
- id = each.get('id')
- cd = each.get('creation_date')
- if len(cd) > 10:
- sql_new = "UPDATE `post` SET `creation_date` = %s WHERE `id` = %s"
- print(cd[:10])
- cursor.execute(sql_new, (cd[:10], id))
- connection.commit()
- with connection.cursor() as cursor:
- sql = "SELECT `id`, `creation_date` FROM `issue`"
- cursor.execute(sql)
- result = cursor.fetchall()
- for each in result:
- print(each.get('creation_date'))
- id = each.get('id')
- cd = each.get('creation_date')
- if len(cd) > 10:
- sql_new = "UPDATE `issue` SET `creation_date` = %s WHERE `id` = %s"
- print(cd[:10])
- cursor.execute(sql_new, (cd[:10], id))
- connection.commit()
- finally:
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement