使用相同方案将 2 个不同数据库中的表合并为一个

发布于 2025-01-13 02:59:25 字数 1623 浏览 2 评论 0原文

我正在尝试将两个数据库合并为一个,这两个数据库都包含相同的方案,但包含唯一的信息。我已经编写了代码将它们合并在一起,但是根据是否包含“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2025-01-20 02:59:25

您可以传递 :id + null ,它将对 id 列计算为 null ,并且它将获得适当的值,因为它被定义为 < code>INTEGER PRIMARY KEY:

sql = "INSERT INTO albums VALUES (:id + null, :nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
for row in output:
    main_cursor.execute(sql, row)

或者,使用 executemany() 以避免 for 循环:

sql = "INSERT INTO albums  VALUES (:id + null, :nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
main_cursor.executemany(sql, output)

You can pass :id + null which will evaluate as null for the column id and it will get the appropriate value since it is defined as INTEGER PRIMARY KEY:

sql = "INSERT INTO albums VALUES (:id + null, :nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
for row in output:
    main_cursor.execute(sql, row)

Or, with executemany() to avoid the for loop:

sql = "INSERT INTO albums  VALUES (:id + null, :nr, :band, :song, :album, :duration)"
main_cursor = db_main.cursor()
main_cursor.executemany(sql, output)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文