仅更新python最后一条记录

时间:2015-03-10 07:13:56

标签: python sql-server insert pyodbc

我是新手python用户,由于某种原因,以下更新无效。

我期望它使用API​​拉取更新,当我使用打印时,它显示我所期望的5条记录。这些是后续处理的2个单独的XML文件,总共包含这5个记录。一个XML有3行,另外2行。

char01
char02
char03
char04
char05

代码使用需要用kyeID和vCode填充的URL。 keyID和vCode存储在表中。一切正常。当我打印时,它打印出5个单独的记录。

现在它真正做的是当它将它更新为SQL时,只有最后一条记录被插入5次。我不知道这是怎么发生的。然而,当我进行插入时,它的效果非常好。

数据库中的最终结果是最后一次记录5次:

char05
char05
char05
char05
char05

import pyodbc
import time
import tempfile
import cPickle
import zlib
import os
from os.path import join, exists
from httplib import HTTPException

import eveapi

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer;DATABASE=TESTDB;Integrated Security=True')
cursor = cnxn.cursor()

cursor.execute("SELECT keyID, vCode FROM characters_apikey")

for row in cursor.fetchall():
    sql_keyID = row.keyID
    sql_vCode = row.vCode

    api = eveapi.EVEAPIConnection()
    auth = api.auth(keyID=sql_keyID, vCode=sql_vCode)

    result2 = auth.account.Characters()


    for character in result2.characters:
        print (character.name)
        cursor.execute("update characters set characterName = ?, characterID = ?, corporationName =?, corporationID = ?, allianceID = ?, allianceName = ?, factionID = ?, factionName =? ;", (character.name, character.characterID, character.corporationName, character.corporationID, character.allianceID, character.allianceName, character.factionID, character.factionName))
    cursor.commit()

这次更新在这里做错了什么?

1 个答案:

答案 0 :(得分:4)

电话应该类似于:

cursor.execute("update characters set characterName = ?, characterID = ?, corporationName =?, corporationID = ?, allianceID = ?, allianceName = ?, factionID = ?, factionName =? where keyID = ? and vCode = ?;", (character.name, character.characterID, character.corporationName, character.corporationID, character.allianceID, character.allianceName, character.factionID, character.factionName, row.keyID, row.vCode))

只是提醒你错过了where子句。