python连接mysql实例分享

python连接mysql实例分享,第1张

概述示例一#coding=UTF-8importsysimportMySQLdbimporttimereload(sys)sys.setdefaultencoding(\'utf-8\')defconnectDemo():

示例一

#Coding=UTF-8import sysimport MysqLdbimport timereload(sys)sys.setdefaultencoding('utf-8')def connectDemo():  return MysqLdb.Connection("127.0.0.1","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,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',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 #重置游标位置,为偏移量,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 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实例分享全部内容,希望文章能够帮你解决python连接mysql实例分享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1203597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存