gpt4 book ai didi

python & MySQL : unsupported operand type(s) for -: 'int' and 'tuple'

转载 作者:行者123 更新时间:2023-11-29 03:02:25 25 4
gpt4 key购买 nike

我想从表中获取一条记录并将其放入变量“gl”中。如果值大于或小于 value+-gl 它应该写入数据库

import MySQLdb
import time
import string


while True:
db = MySQLdb.connect(host="10.0.0.100", port=3306, user="ubuntu", passwd="ubuntu", db="test")
cursor = db.cursor()
cursor.execute("SELECT glaettung FROM ttable ORDER BY id DESC LIMIT 1")
gl = cursor.fetchall()
print gl
value = (20)
if (value < (value-gl)):
cursor = db.cursor()
cursor.execute("INSERT INTO ttable (value) VALUES(%s)", (value))
elif (value > (value+gl)):
cursor = db.cursor()
cursor.execute("INSERT INTO ttable (value) VALUES(%s)", (value))
else:
print "Done"

这是错误:

ubuntu@ubuntu-Aspire-6920:~/Dokumente$ python test.py 
(('2',),)
Traceback (most recent call last):
File "test.py", line 13, in <module>
if (value < (value-gl)):
TypeError: unsupported operand type(s) for -: 'int' and 'tuple'

最佳答案

gl 是一个元组,要访问其中的 '2',您需要使用索引,然后在其上调用 int()一个整数:

>>> gl = (('2',),)
>>> gl[0][0]
'2'

因此,不要直接使用 gl,而是使用:

>>> int(gl[0][0])
2

关于 python & MySQL : unsupported operand type(s) for -: 'int' and 'tuple' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21051233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com