繁体   English   中英

如何在Python(sqlite)中返回游标?

[英]How to return cursor in Python (sqlite)?

我正在尝试使用Python(2.7)和sqlite(3)将查询结果复制到表中。 因为查询的结果很大,所以我想分批使用“ fetchmany”。 该查询工作正常,也可以分批检索结果。 问题是,当我尝试将结果复制到表中时,它在第一批之后停止。

我怀疑问题出在光标所在的位置。

如何在python中返回光标?

PS:我在这里看到了许多有关游标(关闭)的信息,但是还没有看到我的问题的答案。 另请注意,我是Python的新手,如果问题不大,敬请原谅。

这是我的部分代码:(示例)

                import sqlite3

                dbLocation = 'P:/XXX/db1.db'
                connection = sqlite3.connect(dbLocation)
                cursor = connection.cursor()

                strSQLStatement = """
                        SELECT
                            whatever1,
                            whaterver2
                        from wherever
                        LIMIT 10"""

                cursor.execute(strSQLStatement)


                #the following codes works 
                # printing the 10 results


                while True:
                    results = cursor.fetchmany(2)
                    if not results:
                      break
                    print results   


                #the following codes does NOT work  
                # Only 2 results are processed 

                while True:
                    results = cursor.fetchmany(2)
                    if not results:
                      break
                    print results   
                    cursor.executemany ('INSERT INTO NewTable (?,?)',results)
                    connection.commit()

在原始游标上对executemany()调用掩盖了之前的内容。 创建第二个光标以执行插入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM