python MySQLdb 對mysql基本操作方法

來源:互聯網
上載者:User

標籤:python   nbsp   any   man   roo   cursor   方法   操作   log   

 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import MySQLdb 4  5 conn = MySQLdb.connect(host=‘192.168.1.101‘,user=‘root‘,passwd=‘123‘,db=‘host‘) 6 cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) 7 reCout = cur.execute(‘select ip,name from host,user where user.name = "alex" and user.id=host.id‘) 8 nRet = cur.fetchall() 9 conn.commit()10 cur.close()11 conn.close()12 print reCout13 print nRet14 for i in  nRet:15     print i[‘name‘],i[‘ip‘]16 17 """18 #修改19 conn = MySQLdb.connect(host=‘192.168.1.101‘,user=‘root‘,passwd=‘123‘,db=‘host‘)20 cur = conn.cursor()21 reCout = cur.execute(‘update host set id=%s‘,(1,))22 conn.commit()23 cur.close()24 conn.close()25 print reCout26 """27 """28 #刪除29 conn = MySQLdb.connect(host=‘192.168.1.101‘,user=‘root‘,passwd=‘123‘,db=‘host‘)30 cur = conn.cursor()31 reCout = cur.execute(‘delete from host‘)32 conn.commit()33 cur.close()34 conn.close()35 print reCout36 """37 """38 l = [39     (‘192.168.1.107‘,‘2‘),40     (‘192.168.1.108‘,‘2‘),41     (‘192.168.1.109‘,‘2‘),42     (‘192.168.1.177‘,‘2‘),43 ]44 #插入多條資料45 conn = MySQLdb.connect(host=‘192.168.1.101‘,user=‘root‘,passwd=‘123‘,db=‘host‘)46 cur = conn.cursor()47 reCout = cur.executemany(‘insert into host(ip,id) values(%s,%s)‘,l)48 49 conn.commit()50 cur.close()51 conn.close()52 print reCout53 """54 """55 #插入單條資料56 conn = MySQLdb.connect(host=‘192.168.1.101‘,user=‘root‘,passwd=‘123‘,db=‘host‘)57 cur = conn.cursor()58 reCout = cur.execute(‘insert into host(ip,id) values(%s,%s)‘,(‘192.168.1.1‘,‘1‘))59 60 conn.commit()61 cur.close()62 conn.close()63 print reCout64 """

 

python MySQLdb 對mysql基本操作方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.