#!/usr/bin/python # ================================================================== # test MySQLdb # ------------------------------------------------------------------ # sudo apt-get install python-mysqldb # sudo apt-get install python3-mysqldb # ================================================================== import MySQLdb as mydb def RowCount(cur,table): ##print('RowCount({})'.format(table)) sql = 'SELECT count(*) FROM {}'.format(table) cur.execute(sql) count = cur.fetchone() ##print(type(count)) ##print(type(count[0])) return count[0] try: # --- connect to database phonebook db = mydb.connect('localhost','root','root','phonebook') # -- count rows in phonebook.info table cur = db.cursor() c1 = RowCount(cur,'info') ## print('info row count is ()'.format(c1)) # -- delete row sql = "DELETE FROM info WHERE id=6" cur.execute(sql) db.commit() # -- count rows in phonebook.info table c2 = RowCount(cur,'info') ##print('info row count is {}'.format(c2)) except Exception as e: db.rollback() print(e) finally: cur.close() # close all cursors db.close() # close all databases # -- print before and after difference d = c1 - c2 print('{} rows deleted'.format(d))