我有这个问题:

'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中有问题吗?当我获取查询时

最佳答案

Cursor.fetchone返回代表一行的序列(对于您的情况是一个元组)。

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

vmid = c.fetchone()[0]


或使用元组拆包:

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

关于python - 'tuple'对象没有属性'rstrip',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29932783/

10-12 18:29