Python Forum
Sqlite3 how to know when cursor.execute didn't return anything ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite3 how to know when cursor.execute didn't return anything ?
#1
Question 
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 ?
[Image: NfRQr9R.jpg]
Reply
#2
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 :)
[Image: NfRQr9R.jpg]
Reply
#3
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:
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 1,531 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,514 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,531 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  DataFrame operations didn't change orginal HoldYourBreath 5 2,465 Jan-10-2021, 02:01 PM
Last Post: jefsummers
  Variable scope - "global x" didn't work... ptrivino 5 3,067 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  sqlite3 question - execute method with :parameter richalt2 2 7,495 May-20-2019, 05:35 PM
Last Post: woooee
  Best way to return Cursor Data rajuarien 0 1,982 Jul-29-2018, 09:47 PM
Last Post: rajuarien

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020