mysql10.py

#!/usr/bin/python
# ==================================================================
# test MySQLdb
# ------------------------------------------------------------------
# sudo apt-get install python-mysqldb
# sudo apt-get install python3-mysqldb
# ==================================================================

import MySQLdb as mydb

print('Insert Abe Lincoln into phonebook')

try:
    # --- connect to database phonebook

    db = mydb.connect('localhost','root','root','phonebook')

    # -- insert row into phonebook.info

    cur = db.cursor()

    sql = "INSERT INTO info (firstname,lastname,phonenumber) \
            VALUES ('Abe','Lincoln','123-456-6789')"
    cur.execute(sql)
    db.commit()

except Exception as e:
    db.rollback()
    print(e)

finally:
    cur.close()          # close all cursors
    db.close()           # close all databases