2014-01-10 72 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

相關問題