如何连接另一个数据库并从python脚本SQL Lite成功导入数据?

如何连接另一个数据库并从python脚本SQL Lite成功导入数据?,python,sql,sqlite,Python,Sql,Sqlite,所以我通过python的sqlite3模块连接到SQLite。我正在fine_planner.db中创建营养表,我想从我创建的另一个SQLite数据库(usda.db)中的nutr_def导入特定列;我以前在SQL中成功地完成了这个项目,并希望复制该功能。尽管代码运行时没有出现错误,但对数据库的检查显示,虽然美国农业部数据库中的表(nutr_def)正确填充了数据,但膳食计划器(营养素)中的表是空的。数据没有正确导入,我不知道为什么-这段代码应该很简单。任何洞察都将不胜感激 我正在尝试复制其功能

所以我通过python的sqlite3模块连接到SQLite。我正在fine_planner.db中创建营养表,我想从我创建的另一个SQLite数据库(usda.db)中的nutr_def导入特定列;我以前在SQL中成功地完成了这个项目,并希望复制该功能。尽管代码运行时没有出现错误,但对数据库的检查显示,虽然美国农业部数据库中的表(nutr_def)正确填充了数据,但膳食计划器(营养素)中的表是空的。数据没有正确导入,我不知道为什么-这段代码应该很简单。任何洞察都将不胜感激

我正在尝试复制其功能的原始SQL代码:

drop database if exists meal_plan;
create database meal_plan
character set'latin1'
collate 'latin1_swedish_ci';

drop table if exists meal_plan.nutrient;
create table meal_plan.nutrient 
    as (select Nutr_No as nutrient_id,
    nutrdesc as nutrient_name,
    units as units from usda.nutr_def);
用于创建SQLite数据库的Python脚本:

import sqlite3

database_name = 'meal_planner.db'
db = sqlite3.connect(database_name)

db.row_factory = sqlite3.Row

cursor = db.cursor()

cursor.execute("attach database 'usda.db' as usda")
cursor.execute("drop table if exists nutrient")
cursor.execute("create table nutrient(nutrient_id, nutrient_name, units)")
cursor.execute("insert into nutrient select Nutr_No, nutrdesc, units from usda.nutr_def")
[美国农业部数据库,表nutr_def中的数据]


[数据库膳食\u planner,运行代码后营养素表中的数据(为空)]

请将代码粘贴为文本,而不是屏幕快照刚刚完成!谢谢我没有看到commit
execute(“commit”)
的语句。请检查您的代码是否有“提交”步骤。