Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:51+00:00 2026-06-14T18:48:51+00:00

I’m learning to access a MySQL database using the MySQL Connector/Python library downloaded from

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:48:52+00:00Added an answer on June 14, 2026 at 6:48 pm

    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.

    cnx.commit()
    

    You omitted the () parenthesis in your code.

    You also reference the cursor.close method in several locations without calling it; not as big a deal as Python will automatically clean those up through garbage collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.