python连接mysql实例分享
Python  /  管理员 发布于 7年前   193
示例一
#coding=UTF-8import sysimport MySQLdbimport timereload(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 MySQLdbconn = 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 xcursor.close()conn.close()示例三
1 安装Python的Mysql包
[email protected]:~# apt-get install python-mysqldb [email protected]:~# 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 下面就可以连接数据库,可以进行增删改操作.
[email protected]: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() [email protected]:python# ./create.py
3 下面利用python查看mysql里刚添加的记录.
[email protected]: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()
[email protected]: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')
test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
