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

19,146

Solution 1

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.

Solution 2

dwh_cur.fetchone() returns a tuple, try this:

dwh_cur.fetchone()[0]
Share:
19,146
hello kee
Author by

hello kee

Updated on June 07, 2022

Comments

  • hello kee
    hello kee almost 2 years

    I am 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