【发布时间】:2018-01-21 12:48:13
【问题描述】:
类代码:
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'
如果您需要有关此疑问的更多信息,请告知。 :-)
【问题讨论】:
-
我们需要查看
self._execute_query,因为它似乎是返回列表的方法,而不是带有rowcount属性的游标。 -
对不起。刚刚我更新了它。谢谢:-)
标签: python python-3.x list attributeerror postgresql-9.5