简体   繁体   中英

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. 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.

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:

#!/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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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