Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TypeError: can only concatenate str (not "tuple") to str

I'm trying to print the output of SQL output as below:

dwh_cur.execute("""select count (*) from sales""")
var1 = dwh_cur.fetchone()
text = 'Total Sales is ' + var1

var1 = 100

Expected output:

Total Sales is 100

But I get an error

TypeError: can only concatenate str (not "tuple") to str
like image 358
hello kee Avatar asked Mar 05 '23 22:03

hello kee


1 Answers

Well dwh_cur.fetchone() returns a record which is represented as a tuple of n elements,

dwh_cur.execute("""select count (*) from sales""")
var1 = dwh_cur.fetchone()
text = 'Total Sales is {}'.format(var1[0])

Might work depending on what is returned form the query.

like image 174
Tomasz Swider Avatar answered Mar 07 '23 10:03

Tomasz Swider