Search code examples
pythonpython-3.xlistattributeerrorpostgresql-9.5

AttributeError: 'list' object has no attribute 'rowcount'


class code:

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

And the code in the main program:

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

following code conatains _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

I can't figure out why I'm getting this error.

I am trying to get data from database here actually .This is a part of code available in the Github.(PACKAGE QUERY). This is the output I am getting:

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'

Please tell if you need more information about this doubt. :-)


Solution

  • Your _execute_query method returns a list when your query starts with select or 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 is a list, not a cursor, so won't have the attribute. Either return the cursor there, or use len() to get a count of rows.