首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python MySQLdb 对mysql基本操作方法

python MySQLdb 对mysql基本操作方法

作者头像
程序员同行者
发布2018-06-22 16:38:17
4290
发布2018-06-22 16:38:17
举报
文章被收录于专栏:程序员同行者程序员同行者
 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 """
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-01-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档