简体   繁体   English

带Python的Sqlite3返回额外数据

[英]Sqlite3 with Python returning extra data

I am working on a personal project to learn using SQLite3 with Python by creating a dvd inventory application. 我正在一个个人项目上,通过创建DVD库存应用程序来学习将SQLite3与Python结合使用。 I have had some success with my SQLite related functions, which are in a library file, and intended to be called from other portions of the application. 我与SQLite相关的函数取得了一些成功,这些函数位于库文件中,并打算从应用程序的其他部分调用。

I am running into a problem with extraneous data being returned in a query, in this case, the same data twice. 我在查询中返回无关数据时遇到了问题,在这种情况下,相同数据两次。

I am testing a function that simply queries a table, and returns all its values. 我正在测试一个简单查询表并返回其所有值的函数。 If I run that function from a call within the library file it resides in, it works properly, but if I call it from an external file that imports the library file, it returns the data twice, and I am unsure why. 如果我通过驻留在库文件中的调用运行该函数,则它可以正常工作,但是如果我从导入该库文件的外部文件中调用该函数,它将返回两次数据,我不确定为什么。

The function is as follows: 功能如下:

def query_all():
con = db_connect()
cur = con.cursor()
cur.execute('''SELECT film_id, film_name, film_genre, date_added FROM 
film_inv''')
all_rows = cur.fetchall()
for row in all_rows:
    print('{0} : {1}, {2}, {3}'.format(row[0], row[1], row[2], row[3]))
con.close()

When run from within the library file, it returns proper results as: 从库文件中运行时,它将返回以下正确结果:

1 : Star Wars, Comedy, 2018-11-15 12:23:28
2 : Titanic, Drama, 2018-11-15 12:28:55
3 : Cars, Family, 2018-11-15 12:29:18
4 : Christmas Vacation, Holiday, 2018-11-15 12:37:59
6 : Cinderella, Family, 2018-11-15 12:43:13

Process finished with exit code 0

When called via an external file, it returns the results twice: 通过外部文件调用时,它将两次返回结果:

try:
    pydvd_utils.query_all()
except Exception as e:
    print(e)

1 : Star Wars, Comedy, 2018-11-15 12:23:28
2 : Titanic, Drama, 2018-11-15 12:28:55
3 : Cars, Family, 2018-11-15 12:29:18
4 : Christmas Vacation, Holiday, 2018-11-15 12:37:59
6 : Cinderella, Family, 2018-11-15 12:43:13
1 : Star Wars, Comedy, 2018-11-15 12:23:28
2 : Titanic, Drama, 2018-11-15 12:28:55
3 : Cars, Family, 2018-11-15 12:29:18
4 : Christmas Vacation, Holiday, 2018-11-15 12:37:59
6 : Cinderella, Family, 2018-11-15 12:43:13

Process finished with exit code 0

The external file, query_all.py, is simple, only calling the function: 外部文件query_all.py很简单,仅调用以下函数:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pydvd_utils

try:
    pydvd_utils.query_all()
except Exception as e:
    print(e)

Face palm moment! 面对手掌的时刻!

The function was definitely running twice, due to a PEBKAC error. 由于PEBKAC错误,该功能肯定运行了两次。

I had a call of the function I was using for direct testing within the library, and it wasn't commented out. 我在库中调用了用于直接测试的函数,但未将其注释掉。 Calling the function externally was running the function, and then the call to the function that both existed in the library file. 从外部调用该函数将先运行该函数,然后再调用同时存在于库文件中的该函数。

Commenting out the test call to the function in the library resolved the problem. 注释掉对库中函数的测试调用即可解决该问题。

Thank you to everyone for the direction. 谢谢大家的指导。

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

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