Python操作MySQL的一个报错:IndexError: out of range
2011-04-15 13:16
1251 查看
# -*- 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
相关文章推荐
- python PIL, 错误:IndexError: string index out of range
- Python 爬虫 raise self._value IndexError: list index out of range
- MySQL使用like出现 操作MySQL数据库报出:Parameter index out of range (1 > number of parameters
- **Python错误记录:IndexError: list index out of range**
- FAQ(52):java.sql.SQLException: Column Index out of range, 0 > 13. at com.mysql.jdbc.ResultSet.getInt
- mysql error BIGINT UNSIGNED value is out of range in 解决办法
- jsp中向MySQL插入数据出现:Parameter index out of range (1 > number of parameters, which is 0).
- [转]操作MySQL数据库报出:Parameter index out of range (1 > number of parameters, which is
- 【Android】Error:Execution failed for task ':app:mergeDebugResources'. > String index out of range: 0
- 【Bug-python】IndexError: list index out of range
- 遇到的一个"String index out of range" 问题
- python IndexError: list assignment index out of range
- 遇到一个MySQL小问题 Data truncation: Out of range value for column 'huid' at row 1
- tensorflow_python_framework.errors_impl_OutOfRangeErrorFIFOQueue
- 生成web service client时Error generating services (String index out of range: 3)
- py2exe打包报错ndexError: tuple index out of range
- python 脚本IndexError: tuple index out of range
- 【MySQL】Error 1264: out of range value for column
- IndexError: list index out of range的错误原因
- mysql -Column Index out of range