Python操作MySQL的一个报错:IndexError: out of range

 
  
# -*- coding: utf-8 -*-
import sys
import MySQLdb
try :
conn
= MySQLdb.connect(host = " localhost " ,user = " root " ,passwd = " xxx " )
except Exception,e:
print e
sys.exit()
# 获取操作游标
cursor = conn.cursor()
# 执行SQL,创建一个数据库.
cursor.execute( """ create database if not exists zhsen DEFAULT CHARACTER SET utf8 COLLATE utf8_bin """ )
conn.select_db(
' zhsen ' )
# 执行SQL,创建一个数据表.
sql = " create table if not exists test1(name varchar(128),age int(4)) "
cursor.execute(sql)
# cursor.execute("insert into test1(name,age)values('haha',22)")
#
conn.commit()
count = cursor.execute( " select * from test1 " )

# 重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,
cursor.scroll(0,mode='absolute')
# 获取所有结果
results = cursor.fetchall()
if results:
print ' 总共有%s条记录 ' ,count
for r in results:
print r, ' %s,%s ' % (r[0],r[ 1 ])
else :
print " 未检索出记录! "
cursor.close()
conn.close()

当第一次创建表,没有任何记录的时候,cursor.scroll(0,mode='absolute')如果加上,报错:

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\桌面\test.py", line 22, in <module>
    cursor.scroll(0,mode='absolute')
  File "D:\Python26\Lib\site-packages\MySQLdb\cursors.py", line 364, in scroll
    self.errorhandler(self, IndexError, "out of range")
  File "D:\Python26\Lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
IndexError: out of range

转载于:https://www.cnblogs.com/jumahe/archive/2011/04/15/2017024.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值