Python——列表索引超出范围——在一个简单的 SELECT 语句上?

标签 python sql

我是 Python 的新手,所以我可能犯了一个新手错误。但这似乎不是我在这种情况下应该得到的那种错误。

在一个非常简单的 SELECT 语句中,我收到“列表索引超出范围”错误。

  sql = """
        begin tran
        -- Several update statements are in this block.

        commit tran"""
    sql = sql.format(tablename=self.tablename, **self.mappings)
    #print(sql)
    self.cursor.execute(sql, (self.catalog_id,self.catalog_id,self.catalog_id,))


    self.cursor.execute("SELECT CaptionText, DisplayOrder FROM dbo.SizeOrder where CaptionText is not NULL and len(CaptionText)>0")  #This is the line that breaks!

    size_order = {row[0].lower(): row[1] for row in self.cursor}

中断的是倒数第二行。它没有做任何有趣的格式化。我没有在这个问题查询中做任何替换。当直接针对数据库运行时,它会返回超过 300 条记录。

跟踪输出肯定暗示了那行代码。我真的怀疑下一行 b/c 它涉及索引。但执行后返回无济于事。

    Traceback (most recent call last):
  File "import.py", line 782, in <module>
    main(sys.argv)
  File "import.py", line 167, in main
    db.transform_catalog((len(args)>4 and args[4] == "--skipimages") or (len(args)>5 and args[5] == "--skipimages") )
  File "import.py", line 235, in transform_catalog
    self.do_transform(skipImages)
  File "import.py", line 264, in do_transform
    self.insert_size_types()
  File "import.py", line 501, in insert_size_types
    self.cursor.execute("SELECT CaptionText, DisplayOrder FROM dbo.SizeOrder  where CaptionText is not NULL and len(Capt
ionText)>0")
  File "C:\Python33\lib\site-packages\pypyodbc.py", line 1449, in execute
    self._free_stmt(SQL_CLOSE)
  File "C:\Python33\lib\site-packages\pypyodbc.py", line 1971, in _free_stmt
    check_success(self, ret)
  File "C:\Python33\lib\site-packages\pypyodbc.py", line 986, in check_success
    ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
  File "C:\Python33\lib\site-packages\pypyodbc.py", line 951, in ctrl_err
    state = err_list[0][0]
IndexError: list index out of range

我做错了什么?谢谢!

最佳答案

绝对是一个错误。在库内部的某些结果处理过程中出现不相关的错误,与输入无关。 DeepSpace's link是可能的原因。

关于Python——列表索引超出范围——在一个简单的 SELECT 语句上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864360/

相关文章:

python - 重置 pandas 中多级列的索引,以便较高的索引 perfaces 较低的索引

python - 无法加载 uWSGI 插件 : ./python3_plugin.so: 无法打开共享对象文件: 没有这样的文件或目录

python - Django:Heroku sleep 后数据库突然重置

sql - 需要从左连接的表中获取多个值

Python:IMAP 连接到 gmail 返回错误

sql - SQL 中时间段的交叉和合并

mysql - 在表格中间拖动一些行

sql - 无法访问 SQL Server 2005 表中的单行

mysql - 避免返回多行

python - 在python中将字符串的第一个字母更改为大写