zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    flask helloworld
    (16)centos7 日志文件
    (15)centos7 系统服务
    (14)centos7 进程管理
    (13)centos7 任务计划
    (12)centos7 环境变量配置
    [BZOJ2045]双亲数(莫比乌斯反演)
    bzoj2018 [Usaco2009 Nov]农场技艺大赛
    bzoj 1001 [BeiJing2006]狼抓兔子
    bzoj 5056: OI游戏 最短路树的计数
  • 原文地址:https://www.cnblogs.com/jumahe/p/2017024.html
Copyright © 2011-2022 走看看