Python Forum

Full Version: Sqlite3 how to know when cursor.execute didn't return anything ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I have an INNER JOIN command in my python

for exemple
cursor.execute('''
SELECT a1, a2, b1, b2
FROM A
INNER JOIN B on B.f = A.f;
''')
OK now let suppose that the result is nothing. How can I know ?

I have try few thing

cursor.rowcount
but that always return -1 even if I have a result.

also
test = cursor.execute('''
SELECT a1, a2, b1, b2
FROM A
INNER JOIN B on B.f = A.f;
''')
type(test)
Output:
<class 'sqlite3.Cursor'>
Any ideas ?
I've found :) I'll post the solution in the following min.

Tester = test.fetchall()

	if len(Tester) == 0:
		print("Nothing left")
	else:
		for each in Tester:
			print(each)
I don't found it nice, so if you have better I'm all ears :)
If you expect 1 response I would use
if reply := test.fetchone() is not None
If there might be multiple responses, I use cursor's lazy iterator.
for reply in test: