蟒蛇&Mysql:不支持的操作数类型 -:'int' 和 'tuple' [英] python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

查看:45
本文介绍了蟒蛇&Mysql:不支持的操作数类型 -:'int' 和 'tuple'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从表中获取一条记录并将其放入变量gl"中.如果 value 更大或更小,则 value+-gl 应该写入数据库

导入 MySQLdb导入时间导入字符串为真:db = MySQLdb.connect(host="10.0.0.100", port=3306, user="ubuntu", passwd="ubuntu", db="test")游标 = db.cursor()cursor.execute("SELECT glaettung FROM ttable ORDER BY id DESC LIMIT 1")gl = cursor.fetchall()打印gl值 = (20)如果(值<(值-gl)):游标 = db.cursor()cursor.execute("INSERT INTO ttable (value) VALUES(%s)", (value))elif(值>(值+gl)):游标 = db.cursor()cursor.execute("INSERT INTO ttable (value) VALUES(%s)", (value))别的:打印完成"

这是错误:

ubuntu@ubuntu-Aspire-6920:~/Dokumente$ python test.py(('2',),)回溯(最近一次调用最后一次):文件test.py",第 13 行,在 <module> 中如果(值<(值-gl)):类型错误:不支持 - 的操作数类型:'int' 和 'tuple'

解决方案

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

<预><代码>>>>gl = (('2',),)>>>gl[0][0]'2'

所以,不要直接使用 gl,而是使用:

<预><代码>>>>int(gl[0][0])2

I would like to fetch a record from a table and put it in the variable "gl". if value is bigger or smaller then value+-gl it should write into the database

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',),)
>>> gl[0][0]
'2'

So, instead of using gl directly, use:

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

这篇关于蟒蛇&amp;Mysql:不支持的操作数类型 -:'int' 和 'tuple'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆