Python-Ref > Databases and Python > Python and MySQL > Updating data
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Updating data

Expand/Shrink
Zdroj: (mysql5-1.py)
  1   # encoding: utf-8
  2   import MySQLdb
  3   from MySQLdb.cursors import DictCursor
  4   
  5   # connect
  6   connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost", cursorclass=DictCursor)
  7   cursor1 = connection.cursor()
  8   
  9   # find the head of Czech Republic
 10   cursor1.execute( "SELECT HeadOfState FROM Country WHERE Code='CZE';")
 11   print "Old president:", cursor1.fetchone()['HeadOfState']
 12   
 13   # update it to current value
 14   cursor1.execute( (u'UPDATE Country SET HeadOfState="Václav Klaus" WHERE Code="CZE";').encode('utf-8'))
 15   
 16   # find the new head of Czech Republic
 17   cursor1.execute( "SELECT HeadOfState FROM Country WHERE Code='CZE';")
 18   print "New president:", cursor1.fetchone()['HeadOfState']
 19   
 20   cursor1.close()
 21   connection.close()
stdout:
Old president: Václav Havel
New president: Václav Klaus
Doba běhu: 750.1 ms