python连接mysql很慢 2.7_Python 2.7 学习笔记 访问mysql数据库

一、基本概念

使用python操作数据库,其基本的流程如下(其实所有开发语言访问数据库的流程都是这样)。

1、第一,引入相应数据库的python数据库接口模块,针对不同的数据库类型,有不同的数据库访问接口模块。可以理解这些接口模块提供了一些api接口,让python代码可以访问数据库。

2、获取数据库的连接

3、执行sql语句

4、关闭数据库连接

二、下载python数据库接口包

比如对应mysql数据库,首先要下载相应的python mysql包。

可以到 https://pypi.python.org/pypi/MySQL-python/1.2.5 这个链接下下载,

对于windows系统,会有exe安装包,安装后,会在 Python27\Lib\site-packages 目录下生成mysql的包

三、代码编写

表结构如下: create table test(id int,info varchar(100))

#coding=utf-8

importtimeimportMySQLdbtry:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('insert into test values(0,"x0")')

conn.commit()

cur.close()

conn.close()print "finish insert direct"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('insert into test values(%s,%s)',(1,"x1"))

cur.execute('insert into test values(%s,%s)',[2,"x2"])

conn.commit()

cur.close()

conn.close()print "finish insert by para"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

data=[];

data.append((3,"x3"));

data.append((4,"x4"));

cur.executemany('insert into test values(%s,%s)',data)

data=[];

data.append([5,"x5"]);

data.append([6,"x6"]);

cur.executemany('insert into test values(%s,%s)',data)

conn.commit()

cur.close()

conn.close()print "finish muti insert by para"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('select * from test where id=%s',('2')) #数字参数?

res=cur.fetchall()for re inres:print "id=%s,info=%s" % (re[0],re[1])

cur.close()

conn.close()print "finish query where"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('select * from test')

res=cur.fetchall()for re inres:print "id=%s,info=%s" % (re[0],re[1])

cur.close()

conn.close()print "finish query fetchall"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('select * from test') #游标指向第一条记录

re = cur.fetchone() #获取当前游标记录,同时游标指向下一条记录

print "id=%s,info=%s" % (re[0],re[1])

re=cur.fetchone()print "id=%s,info=%s" % (re[0],re[1])

res=cur.fetchall() #获取当前游标记录及后续所有记录

for re inres:print "id=%s,info=%s" % (re[0],re[1])

cur.close()

conn.close()print "finish query fetchone"

exceptMySQLdb.Error,e:print e.args[1]try:

conn= MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)

cur=conn.cursor()

cur.execute('delete from test')

conn.commit()

cur.close()

conn.close()print "finish delete"

exceptMySQLdb.Error,e:print e.args[1]

需要说明的是,如果python脚本的字符集编码与数据库的字符集不一致,中文会出现乱码。

解决方法是在获取链接时指定数据库的字符集。如:

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306,charset='gbk')

上面语句通过 增加 charset='gbk' 指定数据库的字符集。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值