1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportMySQLdb4 5conn = MySQLdb.connect (host='192.168.1.101', user='Root', passwd='123', db='Host')6cur = conn.cursor (cursorclass=MySQLdb.cursors.DictCursor)7Recout = Cur.execute ('Select Ip,name from host,user where user.name = "Alex" and User.id=host.id')8Nret =Cur.fetchall ()9 Conn.commit ()Ten cur.close () One conn.close () A PrintRecout - PrintNret - forIinchNret: the Printi['name'],i['IP'] - - """ - #修改 + conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') - cur = conn.cursor () + recout = Cur.execute (' Update host set id=%s ', (1,)) A Conn.commit () at cur.close () - conn.close () - Print Recout - """ - """ - #删除 in conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') - cur = conn.cursor () to recout = cur.execute (' Delete from host ') + Conn.commit () - cur.close () the conn.close () * Print Recout $ """Panax Notoginseng """ - L = [ the (' 192.168.1.107 ', ' 2 '), + (' 192.168.1.108 ', ' 2 '), A (' 192.168.1.109 ', ' 2 '), the (' 192.168.1.177 ', ' 2 '), + ] - #插入多条数据 $ conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') $ cur = conn.cursor () - recout = Cur.executemany (' INSERT into host (Ip,id) VALUES (%s,%s) ', L) - the Conn.commit () - cur.close ()Wuyi conn.close () the Print Recout - """ Wu """ - #插入单条数据 About conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') $ cur = conn.cursor () - recout = Cur.execute (' INSERT into host (Ip,id) VALUES (%s,%s) ', (' 192.168.1.1 ', ' 1 ')) - - Conn.commit () A cur.close () + conn.close () the Print Recout - """
Python mysqldb basic operation method for MySQL