#!/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 as id 2 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 (id,firstname,lastname,phonenumber) \
VALUES (2,'Abe','Lincoln','123-456-6789')"
cur.execute(sql)
db.commit()
except Exception as e:
print('Oops!')
db.rollback()
print(e)
finally:
cur.close() # close all cursors
db.close() # close all databases