You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 2.7执行SQLite数据库UPDATE操作未达预期,数据表无数据

问题:SQLite UPDATE执行后foo表无数据

我需要在Python 2.7中对SQLite数据库执行UPDATE操作。数据表结构如下:

CREATE TABLE IF NOT EXISTS "foo" ( "country_code" TEXT, "country_name" TEXT, "continent_code" TEXT, "area" INTEGER );

我编写的代码如下:

connection = sqlite3.connect('foo.db')
cursor = connection.cursor()
urlCountriesContinents = 'https://raw.githubusercontent.com/Stophface/geojson-places/master/data/continents/continents.json'
countriesContinents = requests.get(urlCountriesContinents)
countriesContinents = countriesContinents.json()
for continent in countriesContinents:
    continentCode = continent['continent_code']
    for countryCode in continent['countries']:
        cursor.execute('UPDATE country SET country_code = ?, country_name = ?, continent_code = ?', (countryCode, 'Foo', continentCode))
connection.commit()
connection.close()

但执行完成后,执行SELECT * FROM foo查询时,结果集为空,数据表中没有任何数据。请问这是什么原因?


回答

我来帮你分析下问题所在,主要有两个关键错误导致你看不到数据:

1. 表名写错了

你的目标数据表是foo,但UPDATE语句里写的是UPDATE country——SQLite找不到名为country的表,这条语句相当于没有执行任何操作,自然不会对foo表产生任何影响。

2. UPDATE的逻辑误区

UPDATE语句的核心作用是修改表中已存在的行。如果你的foo表一开始是空的(没有任何记录),那UPDATE不会插入新数据,执行后表依然会是空的。

针对场景的解决方案

根据你的代码逻辑,看起来你是想把从接口获取的国家数据写入foo表,这里分两种情况给出方案:

方案1:插入新数据(更符合你的需求)

如果是要新增数据,直接用INSERT语句;如果需要处理重复数据(比如避免相同country_code重复插入),可以用INSERT OR REPLACE(存在则更新,不存在则插入):

connection = sqlite3.connect('foo.db')
cursor = connection.cursor()
urlCountriesContinents = 'https://raw.githubusercontent.com/Stophface/geojson-places/master/data/continents/continents.json'
countriesContinents = requests.get(urlCountriesContinents)
countriesContinents = countriesContinents.json()
for continent in countriesContinents:
    continentCode = continent['continent_code']
    for countryCode in continent['countries']:
        # 使用INSERT OR REPLACE处理重复数据
        cursor.execute('INSERT OR REPLACE INTO foo (country_code, country_name, continent_code) VALUES (?, ?, ?)', (countryCode, 'Foo', continentCode))
connection.commit()
connection.close()

方案2:更新已有数据

如果你确实是要修改foo表中已存在的记录,需要先确保表中有对应country_code的行,同时修正表名:

connection = sqlite3.connect('foo.db')
cursor = connection.cursor()
urlCountriesContinents = 'https://raw.githubusercontent.com/Stophface/geojson-places/master/data/continents/continents.json'
countriesContinents = requests.get(urlCountriesContinents)
countriesContinents = countriesContinents.json()
for continent in countriesContinents:
    continentCode = continent['continent_code']
    for countryCode in continent['countries']:
        # 修正表名,并且通过WHERE指定要更新的行
        cursor.execute('UPDATE foo SET country_name = ?, continent_code = ? WHERE country_code = ?', ('Foo', continentCode, countryCode))
        # 可以通过rowcount查看生效行数,方便调试
        print(f"更新了 {cursor.rowcount} 行数据")
connection.commit()
connection.close()

内容的提问来源于stack exchange,提问作者four-eyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:37:40