Python operation MySQL Database (retrieve/delete data in database)

Source: Internet
Author: User
Tags python mysql

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.