1, ready to install the Python MySQL module "mysqldb" and the Python form module "prettytable" (previous article has been introduced, not repeat here)
[Email protected] ~]# Pythonpython 2.6.6 (r266:84292, Jan, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on Li Nux2type "Help", "copyright", "credits" or "license" for more information.>>> import mysqldb>>> from pre Ttytable Import prettytable # Make sure the module is installed
2. Program Body
[[Email protected] ~]# vim xt_forbidacc_del.py#!/usr/bin/pythonimport os,sys, mysqldbfrom prettytable import prettytablehostip = ' 127.0.0.1 ' username = ' Root ' passwd = ' redhat ' db = ' mysql ' Try:acc = raw_input (' please input Accname: '). Strip () Conn=mysqldb.connect (hostip,username,passwd,db,port=3306) cur=conn.cursor () Cur.execute (" Select host,user,password from user where user= '%s ' " %ACC) jilu = Cur.fetchone () if jilu == none:print ' ######## no such user:\033[33;3m%s\033 [0m '%accsys.exit () else:x = prettytable () x.field_names = [' host ', ' User ', ' password '] X.padding_width = 1x.add_row ([jilu[0],jilu[1],jilu[2]]) print xsure = raw_input (' is you sure delete \033[33;3m%s\033[0m:[y/n] ' %acc). Strip () if sure == ' Y ' or sure == ' Y ': cur.Execute ("delete from user where user= '%s '"  %ACC) Conn.commit () Else:cur.close () Conn.close () Sys.exit () Cur.close () conn.close () except mysqldb.error,e:print "Mysql Error %d: %s " % (E.args[0], e.args[1])
Program Testing
[Email protected] ~]# Mysql-uroot-predhat
mysql> select host,user,password from mysql.user; # Pre-delete database data +-----------+------+--------------- ----------------------------+| host | user | password |+-----------+ ------+-------------------------------------------+| localhost | root | * 84bb5df4823da319bbf86c99624479a198e6eee9 | | lnmp | root | | | 127.0.0.1 | root | | | localhost | | | | lnmp | | | | localhost | shaw | *84bb5df4823da319bbf86c99624479a198e6eee9 |+-----------+------+--------------------- ----------------------+6 rows in set (0.00 SEC)
[[email protected] ~]# python xt_forbidacc_del.py # Delete Account "Shaw" Please input accname: shaw+-----------+------+-------------------------------------------+| host | user | password |+-----------+------+-------------------------------------------+| localhost | shaw | *84bb5df4823da319bbf86c99624479a198e6eee9 |+-----------+------+------------ -------------------------------+are you sure delete shaw:[y/n]y
[[Email protected] ~]# mysql -uroot -predhatmysql> select host,user, password from mysql.user; # database data after deletion +-----------+------+-------------------------------------------+| host | user | password |+-----------+------+----------------------------- --------------+| localhost | root | *84bb5df4823da319bbf86c99624479a198e6eee9 | | lnmp | root | | | 127.0.0.1 | root | | | localhost | | | | lnmp | | |+-----------+------+-------------------------------------------+5 rows in set (0.01 SEC)
Python operation MySQL Database (retrieve/delete data in database)