合并来自两个不同数据库的表(Python)

时间:2022-04-04 09:50:55

I have two databases Database1.db and Database2.db. The databases contain tables with matching names and matching columns (and the Primary key is the 'Date' column in both). The only difference between the two is that the entries in Database1 are from 2013 and the entries in Database2 are from 2014. I would like to merge these two databases so that all the 2013 and 2014 data ends up in one table in a third database (let's call it Database3.db).

我有两个数据库Database1.db和Database2.db。数据库包含具有匹配名称和匹配列的表(主键是两者中的“日期”列)。两者之间的唯一区别是Database1中的条目来自2013年,Database2中的条目来自2014年。我想合并这两个数据库,以便所有2013和2014数据最终都在第三个数据库的一个表中(我们称之为Database3.db)。

To be clear, here is what the databases I'm working with currently contain and what I want the third resulting database to contain:

为了清楚起见,这里是我正在使用的数据库当前包含的内容以及我希望第三个结果数据库包含的内容:

Database1.db:

Database1.db:

Table Name: GERMANY_BERLIN

表名:GERMANY_BERLIN

Date            Morning    Day     Evening    Night
01.01.2013      0.5        0.2      0.2       0.1
02.01.2013      0.4        0.3      0.1       0.2
...

Database2.db:

Database2.db:

Table Name: GERMANY_BERLIN

表名:GERMANY_BERLIN

Date            Morning    Day     Evening    Night
01.01.2014      0.6        0.2      0.1       0.1
02.01.2014      0.5        0.2      0.3       0.0
...

I would like to have create a resulting Database3 with the following data:

我想用以下数据创建一个生成的Database3:

Database2.db:

Database2.db:

Table Name: GERMANY_BERLIN

表名:GERMANY_BERLIN

Date            Morning    Day     Evening    Night
01.01.2013      0.5        0.2      0.2       0.1
02.01.2013      0.4        0.3      0.1       0.2
01.01.2014      0.6        0.2      0.1       0.1
02.01.2014      0.5        0.2      0.3       0.0
...

I haven't been able to find anything directly helpful on this online yet (perhaps JOINS could be used somehow? bhttp://www.tutorialspoint.com/sqlite/sqlite_using_joins.htm) so any suggestions would be greatly appreciated!

我还没有找到任何直接有用的东西在网上(也许JOINS可以用某种方式?btttp://www.tutorialspoint.com/sqlite/sqlite_using_joins.htm)所以任何建议将不胜感激!

PS. SQLite has been used to create the existing databases and is the database-related Python library that I'm most familiar with

PS。 SQLite已被用于创建现有数据库,并且是我最熟悉的与数据库相关的Python库

1 个解决方案

#1


0  

You can easly export a .db into a csv (How to export sqlite to CSV in Python without being formatted as a list?) and import it again into .db (Importing a CSV file into a sqlite3 database table using Python) for the step 1 just append the result to the same cvs file.

您可以轻松地将.db导出到csv(如何在Python中将sqlite导出为CSV而不将其格式化为列表?)并将其再次导入到.db(使用Python将CSV文件导入sqlite3数据库表)中1只是将结果附加到相同的cvs文件。

#1


0  

You can easly export a .db into a csv (How to export sqlite to CSV in Python without being formatted as a list?) and import it again into .db (Importing a CSV file into a sqlite3 database table using Python) for the step 1 just append the result to the same cvs file.

您可以轻松地将.db导出到csv(如何在Python中将sqlite导出为CSV而不将其格式化为列表?)并将其再次导入到.db(使用Python将CSV文件导入sqlite3数据库表)中1只是将结果附加到相同的cvs文件。