2014-01-10 46 views
2

我想从一张表中取出一条记录并将其放入变量“gl”中。 如果值则值偏大或偏小+ -GL应该写入到数据库python&Mysql:不支持的操作数类型为 - :'int'和'元组'

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" 

这是错误:

[email protected]:~/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' 

回答

1

gl是一个元组,访问'2'里面,你需要使用索引然后调用它int()得到一个整数

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

所以,而是采用gl直接使用:

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

谢谢,thas是问题!现在它可以工作:-) – Schnickschnackschabernack

相关问题