Python TypeError:只能将 str(不是“元组”)连接到 str

hel*_*kee 4 python typeerror python-3.x

我正在尝试打印 SQL 输出的输出,如下所示:

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

变量 1 = 100

预期输出:

Total Sales is 100
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误

TypeError: can only concatenate str (not "tuple") to str
Run Code Online (Sandbox Code Playgroud)

Tom*_*der 5

那么 dwh_cur.fetchone() 返回一个记录,它表示为 n 个元素的元组,

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

可能工作取决于从查询返回的内容。


Ath*_*lha 2

dwh_cur.fetchone() 返回一个元组,试试这个:

dwh_cur.fetchone()[0]
Run Code Online (Sandbox Code Playgroud)