I’m learning to access a MySQL database using the MySQL Connector/Python library downloaded from the mysql.org web site. My environment is OS X 10.6, Eclipse Indigo with PyDev, and newly installed versions of MySql (5.5.28, 64-bit), Sequel Pro, and phpMyAdmin.
My test database has one table “Employees”, which has columns “id”, “name”, and “ssn”. Initially, there are two rows: (1, Lucy, 1234) and (2, Alex, 3456).
Here is the Python script. The variable “config” contains valid access information for the database.
import mysql.connector
from mysql.connector import errorcode
config = {…}
try:
cnx = mysql.connector.connect(**config)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong your username or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
print ("original database contents")
cursor = cnx.cursor()
query = ("SELECT name, ssn FROM Employees") # fetches all employees names and ssns
cursor.execute(query)
for (name, ssn) in cursor: # iterates over fetched data
print(name, ssn) # print one employee per line
cursor.close;
# block 1: insert a new employee into row 3
cursor = cnx.cursor()
cursor.execute("INSERT INTO Employees (id, name, ssn) VALUES (3, 'Sam', '5678')")
cnx.commit()
cursor.close()
# block 2: update the name in the first row
cursor = cnx.cursor()
cursor.execute("UPDATE Employees SET name='Ethel' WHERE id=1")
cnx.commit;
cursor.close()
print ("after inserting Sam and updating Lucy to Ethel")
cursor = cnx.cursor()
query = ("SELECT name, ssn FROM Employees") # fetches all employees' names and ssns
cursor.execute(query)
for (name, ssn) in cursor: # iterates over fetched data
print(name, ssn) # print one employee per line
cursor.close;
cnx.close()
print ("end of database test")
Running the Python script on the initial database with the insert, then update yields:
original database contents
(u'Lucy', u'1234')
(u'Alex', u'3456')
after inserting Sam and updating Lucy to Ethel
(u'Ethel', u'1234')
(u'Alex', u'3456')
(u'Sam', u'5678')
end of database test
Viewing the database with phpMyAdmin or Sequel Pro still shows “Lucy” as the name in row 1.
Running the Python script on the initial database with update, then insert (reversing the order of block 1 and block 2 in the script) yields:
original database contents
(u'Lucy', u'1234')
(u'Alex', u'3456')
after inserting Sam and updating Lucy to Ethel
(u'Ethel', u'1234')
(u'Alex', u'3456')
(u'Sam', u'5678')
end of database test
Viewing the database with phpMyAdmin or Sequel Pro now shows “Ethel” as the name in row 1.
Starting with the initial database open in Sequel Pro, I can execute the two queries in either order and get the correct results.
It seems that something related to committing changes to the database is going wrong, but as a newbie, I’m not seeing it. I would appreciate help in diagnosing this problem.
You need to call the commit method to commit your changes to the database, otherwise the transaction is never saved and other connections won’t see the changes.
You omitted the
()parenthesis in your code.You also reference the
cursor.closemethod in several locations without calling it; not as big a deal as Python will automatically clean those up through garbage collection.