【发布时间】:2016-09-29 16:35:53
【问题描述】:
我正在使用 Python 和 SQLite 来操作数据库。
我在数据库 Data 中有一个 SQLite 表 Movies,如下所示:
| ID | Country
+----------------+-------------
| 1 | USA, Germany, Mexico
| 2 | Brazil, Peru
| 3 | Peru
我在同一个数据库中有一个表 Countries,看起来像这样
| ID | Country
+----------------+-------------
| 1 | USA
| 1 | Germany
| 1 | Mexico
| 2 | Brazil
| 2 | Peru
| 3 | Peru
我想将来自数据库 Data 的所有来自秘鲁的电影插入一个新的数据库 PeruData,如下所示
| ID | Country
+----------------+-------------
| 2 | Peru
| 3 | Peru
我是 SQL 新手,在编写正确的查询时遇到了麻烦。
这是我的尝试:
con = sqlite3.connect("PeruData.db")
cur = con.cursor()
cur.execute("CREATE TABLE Movies (ID, Country);")
cur.execute("ATTACH DATABASE 'Data.db' AS other;")
cur.execute("\
INSERT INTO Movies \
(ID, Country) \
SELECT ID, Country
FROM other.Movies CROSS JOIN other.Countries\
WHERE other.Movies.ID = other.Countries.ID AND other.Countries.Country = 'Peru'\
con.commit()
con.close()
很明显,我做错了什么,因为我得到了错误
sqlite3.OperationalError: no such table: other.Countries
【问题讨论】:
-
我无法重现该错误。您是否尝试直接在 sqlite3 中单独执行查询(
SELECT ID, Country ...)? -
你有什么错误吗?