Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用sqlite在python中使用cursor.execute和cursor.fetchall发出问题_Python_Sql_Database_Eclipse_Sqlite - Fatal编程技术网

使用sqlite在python中使用cursor.execute和cursor.fetchall发出问题

使用sqlite在python中使用cursor.execute和cursor.fetchall发出问题,python,sql,database,eclipse,sqlite,Python,Sql,Database,Eclipse,Sqlite,我在返回代码中所有注释的元组列表时遇到问题。以下是我所拥有的: def list_comments(db, limit=None): """return a list of all comments as tuples - tuples are (id, useremail, page, comment) if limit is provided it should be an integer and only that many commen

我在返回代码中所有注释的元组列表时遇到问题。以下是我所拥有的:

def list_comments(db, limit=None):
    """return a list of all comments as tuples

       - tuples are (id, useremail, page, comment)
       if limit is provided it should be an integer and only that
       many comments will be returned
    """

    cursor.execute("SELECT * FROM comments")
    manyresults = cursor.fetchall()

    for row in cursor:
        return row
所以我要做的是从我的评论表中选择所有内容并返回它:

CREATE TABLE comments (
        id integer unique primary key autoincrement,
        useremail text,
        page text,
        comment text,
        FOREIGN KEY(useremail) REFERENCES users(email)
);"""
我对这一切都很陌生,所以如果我完全错了,请让我知道我做错了什么。谢谢

编辑:这是我运行它的测试

 def test_list_comments(self):
    """The list_comments procedure should return a list of tuples
    one for each of the comment entries in the database, each tuple
    should contain """

    clist = interface.list_comments(self.db)

    # we should have the same number of comments as we created
    self.assertEquals(len(clist), len(self.comments), "Wrong number of comments returned from list_units, expected %d, got %d" % (len(self.comments), len(clist)))

    # comments should be in order so the largest id should be first
    self.assertEquals(clist[0][0], self.comments[-1][0], "Wrong comment id first in comment list, expected %d, got %d" % (clist[0][0], self.comments[-1][0]))

    # and the comment list should be ordered by id
    ids = [c[0] for c in clist]
    self.assertEqual(sorted(ids, reverse=True), ids, "List of comments returned is not in large-to-small order: %s" % (ids,))

    # try the limit argument
    clist = interface.list_comments(self.db, 3)
    self.assertEquals(len(clist), 3, "Wrong number of comments returned from list_comments with a limit argument, expected 3, got %d" % (len(clist),))

你有几个问题:

  • 您没有对cursor.fetchall()的结果执行任何操作。
  • 您只返回第一个结果,而不是所有结果
  • 我想你需要的是:

    cursor.execute("SELECT * FROM comments")
    manyresults = cursor.fetchall()
    return list(manyresults)
    
    尽管您也可以这样做:

    return list(cursor.execute("SELECT * FROM comments"))
    

    哦,这更有道理,我不知道为什么我以前没看到。使用我添加到原始问题描述中的测试,我仍然以某种方式遇到异常错误。.self.assertEquals(clist[0][0],self.comments[-1][0],“注释列表中第一个错误的注释id,应为%d,得到%d”%(clist[0][0],self.comments[-1][0])断言错误:注释列表中第一个错误的注释id,应为0,Get 11您需要添加“ORDER BY id”以进行查询,否则结果将不会按任何内容排序。仍然不会更改测试在预期为0时获得11。谢谢你的帮助,我很感激!如果您正在执行“订购者”,则最大的id将是最后一个。您需要“order by id desc”首先拥有最大的id。