SQLite3更新表中具有新数据的所有行

时间:2019-04-26 12:12:03

标签: python-2.7 sqlite

我正在尝试使用表中每个条目的新值更新表。

我有一个更新脚本,如下所示,我想更新特定表中的所有数据,当前所有行都被1条目的副本替换。 (如下所示)

apList = []

for ap in unifi3.get_aps():
    apList.append(ap)

def update_ap(conn,ap):
        sql = ''' UPDATE aps_york
                  SET name = ?,
                      users = ?,
                      uptime = ?,
                      tx_bytes = ?,
                      rx_bytes = ?
              '''
        cur = conn.cursor()
        cur.execute(sql,ap)
        conn.commit()

def create_connection(db_file):
        try:
                conn = sqlite3.connect(db_file)
                return conn
        except Error as e:
                print e

        return None

def main():
        database = "stormunifi.db"

        conn = create_connection(database)
        for ap in apList:
            row = (ap.get('name'),ap.get('guest-num_sta'),ap.get('uptime'),ap.get('tx_bytes'),ap.get('rx_bytes'))
            update_ap(conn,row)





if __name__ == '__main__':
        main()

下面是我第一次插入时的内容,这是我每次在上面运行更新脚本时要使用新数据进行更新的内容。

Bus 2 - 1391 LTE|1|3526|29737463457|3055529220
Bus 5 - 1393 LTE|8|13682|25377934110|2597146741
Bus 1 - 1394 LTE|0||15240277899|1405514618
Bus 3 - 1395 LTE|1|7777|4096583815|737691175
Bus 4 - 1392 LTE|0||1173064193|96554145

下面是我运行上面显示的更新脚本时发生的情况。

Bus 4 - 1392 LTE|0||1173064193|96554145
Bus 4 - 1392 LTE|0||1173064193|96554145
Bus 4 - 1392 LTE|0||1173064193|96554145
Bus 4 - 1392 LTE|0||1173064193|96554145
Bus 4 - 1392 LTE|0||1173064193|96554145

我不确定为什么只用1个条目替换所有行。我是SQL和python的新手,任何帮助将不胜感激:)

谢谢!

0 个答案:

没有答案