简体   繁体   English

python&mysql:-:'int'和'tuple'不支持的操作数类型

[英]python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

I would like to fetch a record from a table and put it in the variable "gl". 我想从表中获取一条记录并将其放入变量“ gl”中。 if value is bigger or smaller then value+-gl it should write into the database 如果value更大或更小,则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"

This is the error: 这是错误:

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 is a tuple, to access '2' inside it you need to use indexing and then call int() on it get an integer: gl是一个元组,要访问其中的'2' ,您需要使用索引,然后对其调用int()以获取一个整数:

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

So, instead of using gl directly, use: 因此,不要直接使用gl ,而是使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Python TypeError:不支持的操作数类型“int”和“tuple” - How to fix Python TypeError: unsupported operand type(s) 'int' and 'tuple' 类型错误:% 不支持的操作数类型:&#39;tuple&#39; 和 &#39;int&#39; - TypeError: unsupported operand type(s) for %: 'tuple' and 'int' TypesError:+ 不支持的操作数类型:“int”和“tuple” - TypesError: unsupported operand type(s) for +: 'int' and 'tuple' 类型错误:不支持 / 的操作数类型:&#39;tuple&#39; 和 &#39;int&#39; - TypeError: unsupported operand type(s) for /: 'tuple' and 'int' +:&#39;int&#39;和&#39;tuple&#39;的不支持的操作数类型 - unsupported operand type(s) for +: 'int' and 'tuple' TypeError:+不支持的操作数类型:“ int”和“ tuple” - TypeError: unsupported operand type(s) for +: 'int' and 'tuple' Python:/:&#39;tuple&#39;和&#39;float&#39;不支持的操作数类型 - Python: unsupported operand type(s) for /: 'tuple' and 'float' Python 不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; - Python unsupported operand type(s) for /: 'str' and 'int' 不支持 / 的操作数类型:python 中的“list”和“int” - unsupported operand type(s) for /: 'list' and 'int' in python Python-+不支持的操作数类型:“ int”和“ list” - Python - Unsupported Operand type(s) for +: 'int' and 'list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM