Advertisement
Guest User

Update Query

a guest
Dec 28th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. import mysql.connector
  2. import re
  3.  
  4. conn = mysql.connector.Connect(host='localhost', user='root', password='secret', database='sakila', auth_plugin='mysql_native_password')
  5.  
  6. cursor = conn.cursor()
  7. cursor2 = conn.cursor()
  8.  
  9. query = "SELECT ID, Info FROM world_x.city"
  10.  
  11. cursor.execute(query)
  12.  
  13. result = cursor.fetchall()
  14.  
  15. for row in result:
  16.     strw = str(row[1])
  17.     Population = re.findall('\d+', strw)[0]
  18.     query2 = "UPDATE world_x.city SET Population={} WHERE ID={}".format(Population, row[0])
  19.     cursor2.execute(query2)
  20.     conn.commit()
  21.     print("Record updated successfully")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement