AttributeError:' list'对象没有属性' rowcount'

时间:2018-01-21 12:48:14

标签: python python-3.x list attributeerror postgresql-9.5

班级代码:

def rowcount(self):
    return self._psql_cur.rowcount

主程序中的代码:

def sql_query(self, sql, *data, **kwdata):
    """
    NOTE: This function returns a generator. So if you use it to do any kind of update to the dbms that doesn't
    return anything, it won't be executed!
    """

    self.last_select_id += 1

    n_retrials = kwdata.get("___n_retrials", 0)
    if n_retrials > 10:
        raise OperationalError

    assert not (len(data) > 0 and len(set(kwdata) - {"___n_retrials"}) > 0), \
        "Pass either keyword-based data or comma-separated data."

    time_start = time.time()
    n_records_retrieved = 0
    status = None
    toclose = False
    print "*********************inside db.py******************"
    if self.logfile is not None:
        self.logfile.write(">>> {} {} {} START SELECT\n{}\ndata={}\nkwdata={}\n\n".format(
            self.cursor_id, self.last_select_id, time_start, sql, data, kwdata))

    print "\n************* QUERY:\n", sql
    print "\n***************************"

    try:
        if len(data) > 0:
            print "\n**************printing data***********",data
            print "\n******************printing sql**************************",sql
            print "\n*******************************************************"
            # self._psql_cur.execute(sql, data)
            cur, toclose = self._execute_query(sql, data)
        elif len(kwdata) > 0:
            # self._psql_cur.execute(sql, kwdata)
            cur, toclose = self._execute_query(sql, kwdata)
        else:
            cur, toclose = self._execute_query(sql, None)
        print "################check###################"
        n_records_reported = cur.rowcount
        print "\n*************printing rowcount**********",n_records_reported
        # Yield records
        for record in cur:
            n_records_retrieved += 1
            if n_records_retrieved == n_records_reported:
                status = "Finished"
            yield record

以下代码包含_execute_query

def _execute_query(self, sql, args):
    # sql = sql.lower().strip()
    # print sql
    sql_strip = sql.lower().strip()
    print "-------4",args
    # print self.dbname, sql_strip
    if sql_strip.startswith("select ") or \
            (sql_strip.startswith("with ")
             # and "update " not in sql_strip and "insert " not in sql_strip
             ):
        # Try to close previous named cursor
        # if self._psql_cur is not None and not self._psql_cur.closed:
        # try:
        #   self._psql_cur.close()
        # except ProgrammingError:
        #   pass

        # self._psql_cur.scroll(self._psql_cur.rowcount, mode="absolute")
        # self._psql_cur.fetchone()
        # self._psql_cur.fetchone()

        # Create a new named cursor
        self._psql_cur = self.connection.get_cursor()

        print self.dbname, "NAMED", self._psql_cur

        # Execute query
        self._psql_cur.execute(sql, args)
        rows = self._psql_cur.fetchall()
        print "FETCHED RESULT: ", rows
        print sql
        return rows, True
        #self._psql_cur.execute("""select * from recipes""")
        #rows=self._psql_cur.fetchall()
        #print "---------------5  ",rows[0]

        #self._psql_cur.fetchall()
        return self._psql_cur, True
    else:
    # if "insert " in sql or "update " in sql or "delete " in sql or "create" in sql:
    #   print self.dbname, "UNNAMED"
        # In this case, do not use the named (server side) cursor
        # self._psql_unnamed_cur = self._connection.get_cursor(named=False)
        self._psql_unnamed_cur.execute(sql, args)
        return self._psql_unnamed_cur, False

我无法弄清楚为什么我会收到此错误。

我正在尝试从数据库中获取数据。这是Github中可用代码的一部分。(PACKAGE QUERY)。这是我得到的输出:

Exception occured while executing query:


   File "src/dbms/db.py", line 378, in sql_query
     n_records_reported = cur.rowcount
AttributeError: 'list' object has no attribute 'rowcount'

Exception during experiment
'list' object has no attribute 'rowcount'

请告诉您是否需要有关此疑问的更多信息。 : - )

1 个答案:

答案 0 :(得分:0)

当您的查询以_execute_queryselect开头时,您的with方法会返回一个列表:

if sql_strip.startswith("select ") or \
        (sql_strip.startswith("with ")
         # and "update " not in sql_strip and "insert " not in sql_strip
         ):

    # ...

    rows = self._psql_cur.fetchall()
    print "FETCHED RESULT: ", rows
    print sql
    return rows, True

rows是一个列表,而不是游标,因此不具有该属性。将光标返回到那里,或使用len()获取行数。