zoukankan      html  css  js  c++  java
  • python MySQLdb 对mysql基本操作方法

     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 reCout
    13 print nRet
    14 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 reCout
    26 """
    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 reCout
    36 """
    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 reCout
    53 """
    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 reCout
    64 """
  • 相关阅读:
    使用nexus 管理pip 私有包
    gitingore && opensource license 自动生成的网站
    lua-resty-qless-web UI 界面运行
    自定义pip 包开发简单说明
    ethr 微软开源的tcp udp http 网络性能测试工具
    openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
    masterlab 敏捷项目管理工具
    luarocks 自定义包发布试用
    vorpal 又一个方便的cli 开发包
    gogs wekan 集成试用
  • 原文地址:https://www.cnblogs.com/guigujun/p/6240138.html
Copyright © 2011-2022 走看看