使用相同方案将 2 个不同数据库中的表合并为一个
我正在尝试将两个数据库合并为一个,这两个数据库都包含相同的方案,但包含唯一的信息。我已经编写了代码将它们合并在一起,但是根据是否包含“id”(两个表中的主键),我会收到错误。我读过,当将表合并在一起时,我应该只维护其中一个表中的主键,并将其作为自动增量来设置下一个表中的主键。当我完成此操作时,我收到一条错误消息,指出我提供的值列比表所需的少一列,但是当我包含“id”时,我收到唯一 id 的错误(因为两个程序中的主键都是准确的)相同的)。如果有人能告诉我为什么我的主键没有因此增加,我将非常感激。如果我描述得不好或者没有提供足够的信息,请告诉我,我会在需要的地方进行补充。
#正在导入的表的表结构
def build_case_study_1_table():
with sqlite3.connect('albums1.db') as db:
db.execute(
"CREATE TABLE IF NOT EXISTS albums(" \
"id INTEGER PRIMARY KEY NOT NULL," \
"nr INTEGER NOT NULL," \
"band TEXT NOT NULL," \
"song TEXT NOT NULL," \
"album TEXT NOT NULL," \
"duration TEXT NOT NULL);")
#正在合并的表,我的主表
conn = sqlite3.connect('albums.db')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS albums (
id INTEGER PRIMARY KEY NOT NULL,
nr INTERGER NOT NULL,
band TEXT NOT NULL,
song TEXT NOT NULL,
album TEXT NOT NULL,
duration TEXT NOT NULL
)""")
#合并两个表的代码
def merge_databases():
db_main = sqlite3.connect('albums.db')
db_import = sqlite3.connect('albums1.db')
import_cursor = db_import.cursor()
import_cursor.execute('SELECT * FROM albums')
output = import_cursor.fetchall()
sql = "INSERT INTO albums (nr, band, song, album, duration) VALUES (:nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
for row in output:
main_cursor.execute(sql, row)
db_main.commit()
import_cursor.close()
main_cursor.close()
I'm attempting to merge two databases into one with both databases containing the same scheme, but unique information. I've written a code to merge them together, but I get an error depending on whether I include 'id' (the primary key in both tables) or not. I've read that when merging tables together I should only maintain the the primary key in one of the tables and have it as an auto increment to set the primary key in the next table. When I've done this I get an error saying I've supplied one less column of value than the table requires, but when I include 'id' I get an error for unique id's (because the primary key's in both programs are the exact same). If someone can tell me why my primary key isn't incrementing for this I would really appreciate it. If I described anything poorly or didn't present enough information let me know and I'll supplement where needed.
#Table being imported's table structure
def build_case_study_1_table():
with sqlite3.connect('albums1.db') as db:
db.execute(
"CREATE TABLE IF NOT EXISTS albums(" \
"id INTEGER PRIMARY KEY NOT NULL," \
"nr INTEGER NOT NULL," \
"band TEXT NOT NULL," \
"song TEXT NOT NULL," \
"album TEXT NOT NULL," \
"duration TEXT NOT NULL);")
#Table being merged into, my main table
conn = sqlite3.connect('albums.db')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS albums (
id INTEGER PRIMARY KEY NOT NULL,
nr INTERGER NOT NULL,
band TEXT NOT NULL,
song TEXT NOT NULL,
album TEXT NOT NULL,
duration TEXT NOT NULL
)""")
#Code to merge the two
def merge_databases():
db_main = sqlite3.connect('albums.db')
db_import = sqlite3.connect('albums1.db')
import_cursor = db_import.cursor()
import_cursor.execute('SELECT * FROM albums')
output = import_cursor.fetchall()
sql = "INSERT INTO albums (nr, band, song, album, duration) VALUES (:nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
for row in output:
main_cursor.execute(sql, row)
db_main.commit()
import_cursor.close()
main_cursor.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以传递
:id + null
,它将对id
列计算为null
,并且它将获得适当的值,因为它被定义为 < code>INTEGER PRIMARY KEY:或者,使用
executemany()
以避免for
循环:You can pass
:id + null
which will evaluate asnull
for the columnid
and it will get the appropriate value since it is defined asINTEGER PRIMARY KEY
:Or, with
executemany()
to avoid thefor
loop: