Matthew Pace
Matthew Pace

Reputation: 155

Why is my sqlite3 query returning empty rows?

I know I have to be making a dumb mistake here...

            checksum = self.hashItem(os.path.join(root, filename))
            print checksum
            query = 'SELECT * FROM tracked_file WHERE md5 = ?'
            self.con.execute(query, [str(checksum)])
            results = self.cur.fetchall()
            print results

Here's the table structure

'CREATE TABLE if not exists tracked_file (filename TEXT, filepath TEXT, date_created TEXT, date_modified TEXT, file_size INTEGER, sha1 TEXT, md5 TEXT, catalog_date TEXT, archive_date TEXT);'

and the output of the script is showing md5 values I can verify are in the table manually.

48f3dd221a0cb5284c678b3aff24d668 [] ff12917ca9ec1cf8bc913efdf7f3ade7 [] 7b5cf763ef1a75eceb0bb960421a3ec2 [] 390228bde6440ed38f45dbf8ea09d860 [] c10e728e60e580b0eb97c3ab705b8fb9

Upvotes: 0

Views: 853

Answers (1)

Simon Fraser
Simon Fraser

Reputation: 2818

You're running the query against the connection:

self.con.execute(query, [str(checksum)])

But trying to get results from the cursor:

results = self.cur.fetchall()

The connection's execute method creates an intermediate cursor to run the SQL, but this won't be the same cursor you've created and stored in self.cur

Try:

self.cur.execute(query, [str(checksum)])
results = self.cur.fetchall()

Upvotes: 2

Related Questions