python连接mysql实例分享_python
2023-09-14 08:58:26 时间
示例一
#coding=UTF-8 import sys import MySQLdb import time reload(sys) sys.setdefaultencoding('utf-8') def connectDemo(): return MySQLdb.Connection("127.0.0.1","root","root","demo",3306,charset="utf8") if __name__ == '__main__': begin=time.time() conn=connectDemo() cursor = conn.cursor() sql=""" show tables """ count = cursor.execute(sql) rows = cursor.fetchall() cursor.close() conn.close() print "========demo库共:%s 张表============" % (count) print '耗时:%s 秒' % (time.time()-begin)
示例二
import MySQLdb conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="test") cursor = conn.cursor() cursor.execute("select * from hard") res = cursor.fetchall() for x in res: print x cursor.close() conn.close()
示例三
1 安装Python的Mysql包
root@10.1.1.45:~# apt-get install python-mysqldb root@10.1.1.45:~# python Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
这里导入MySQLdb没有报错,就说明安装成功.
2 下面就可以连接数据库,可以进行增删改操作.
root@10.1.1.45:python# cat create.py #!/usr/bin/env python #coding=utf-8 #导入相关模块 import MySQLdb #建立和mysql数据库的连接 conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe') #获取游标 curs = conn.cursor() #执行SQL,创建一个数据库 curs.execute("create database pythondb") #选择连接哪个数据库 conn.select_db('pythondb') #执行SQL,创建一个表 curs.execute("create table test(id int,message varchar(50))") #插入一条记录 value = [1,"davehe"] curs.execute("insert into test values(%s,%s)",value) #插入多条记录 values = [] for i in range(20): values.append((i,'hello mysqldb' + str(i))) curs.executemany("insert into test values(%s,%s)",values) #提交修改 conn.commit() #关闭游标连接,释放资源 curs.close() #关闭连接 conn.close() root@10.1.1.45:python# ./create.py
3 下面利用python查看mysql里刚添加的记录.
root@10.1.1.45:python# cat select.py #!/usr/bin/env python #coding=utf-8 #导入相关模块 import MySQLdb #建立和mysql数据库的连接 conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226') #获取游标 curs = conn.cursor() #选择连接哪个数据库 conn.select_db('pythondb') #查看共有多少条记录 count = curs.execute('select * from test') print "一共有%s条记录" % count #获取一条记录,以一个元组返回 result = curs.fetchone() print "当前的一条记录 ID:%s message:%s" % result #获取后10条记录,由于之前执行了getchone(),所以游标已经指到第二条记录,下面也就从第二条记录开始返回 results = curs.fetchmany(10) for r in results: print r #重置游标位置,0,为偏移量,mode = relative(默认) curs.scroll(0,mode='absolute') #获取所有记录 results = curs.fetchall() for r in results: print r #提交修改 conn.commit() #关闭游标连接,释放资源 curs.close() #关闭连接 conn.close()
root@10.1.1.45:python# ./select.py 一共有21条记录 当前的一条记录 ID:1 message:davehe (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (1L, 'davehe') (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (10L, 'hello mysqldb10') (11L, 'hello mysqldb11') (12L, 'hello mysqldb12') (13L, 'hello mysqldb13') (14L, 'hello mysqldb14') (15L, 'hello mysqldb15') (16L, 'hello mysqldb16') (17L, 'hello mysqldb17') (18L, 'hello mysqldb18') (19L, 'hello mysqldb19')
以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索python , mysql实例 , python3.5连接mysql python3连接mysql python mysql 实例、vb连接mysql实例、android连接mysql实例、php连接mysql实例、vs2010连接mysql实例,以便于您获取更多的相关知识。
相关文章
- python递归函数讲解_Python递归函数实例讲解
- MySQL在Linux上的安装与配置(mysql安装linux)
- 技术实现的网络系统PHP和MySQL结合:搭建网络系统的利器.(基于php和mysql)
- 解析MySQL实例解析:简单易懂的指导(mysql实例案例)
- MySQL: 解析及应用实例(mysql作用)
- MySQL主从同步:配置实例指南(mysql主从同步配置)
- MySQL实例重启步骤详解(mysql重启实例)
- 实用MySQL数据库转换工具推荐(mysql数据库转换工具)
- 如何在 MySQL 中获取表的主键信息?(mysql获取表的主键)
- MySQL分页原理:实现最大限度的效率(mysql的分页原理)
- 查询MySQL表查询实例简易指南(mysql怎么表)
- MySQL事务实现安全性、一致性与可重复性(mysql事务实例)
- 如何应对MySQL删库事件并进行数据恢复?(mysql删库恢复)
- MySQL:哪个版本最适合你?(mysql用哪个版本好)
- MySQL添加字段注释:让你的数据更清晰(mysql 添加字段注释)
- MySQL与Python的协同运行:一种全新的开发体验(mysql与python)
- MySQL中NOT操作的使用方法与实例分析(mysql中not)
- MySQL求差集简便方法两表比较(mysql 两表求差集)
- MySQL如何避免导入指定表(mysql 不导入某张表)
- MySQL下载需注册账号(mysql下载要注册)
- MySQL限制无法启用多个实例(mysql 不支持多实例)
- Python开发MySQL数据库,快速操作实现的mysqldb模块(mysql_db模块)