'tuple'对象没有'rstrip'属性

时间:2015-04-29 01:23:10

标签: python django tuples

我遇到了这个问题:

'tuple' object has no attribute 'rstrip'

我的脚本是:

def destroyVM(request,id):
    conn = sqlite3.connect('testDB')
    c = conn.cursor()   
    api = 'http://196.203.216.18:5555/client/api'
    apikey = '3U7UPLR4JHYEzOhKe0JYnR_ICChdmuynnl0SdqmFzFgG4Hgd6BJHHjcSk2K3SsXUvheLEoN-SlRuEOpqZbqbVA'
    secret = 'JU1gQk99VpAEZWWKxiR97w8mIPtSZsXwuORL6H2NKkve0EHDvBZ5IQN-CXHqMhVKmOLRcRhjyr4RPnTqAmD5Ig'
    cloudstack = CloudStack.Client(api, apikey, secret) # ouvrir une connexion serveur/client
    c.execute("SELECT vmid FROM vmm_vm where id ="+id+";");
    conn.commit()
    vmid = c.fetchone()
    vm = cloudstack.destroyVirtualMachine({'id':vmid})

    c.execute("DELETE FROM vmm_vm WHERE id = '"+id+"';")

    conn.commit()

    conn.close()

    return render_to_response('vms.html',{'vms':Vm.objects.all()})

是vmid的问题吗?当我获取查询 enter image description here

1 个答案:

答案 0 :(得分:3)

Cursor.fetchone返回一个表示行的序列(一个元组)。

您需要从元组中提取项目。

vmid = c.fetchone()[0]

或使用元组解包:

vmid, = c.fetchone()  # same as (vmid,) = c.fetchone()