如何从sqlite中的另一个数据库导入表?

时间:2022-05-02 07:11:35

I have SQLite databases named database1 with a table t1 and database2 with a table t2. I want to import table t2 from database2 into database1. What command(s) should I use?

我有一个名为database1的SQLite数据库,其中包含一个表t1和一个带有表t2的database2。我想将表t2从database2导入到database1中。我应该使用什么命令?

3 个解决方案

#1


30  

Open database2 with the sqlite3 command-line tool and read the table definition with the command .schema t2. (Alternatively, use any other tool that allows to read the table definition.)

使用sqlite3命令行工具打开database2,并使用命令.schema t2读取表定义。 (或者,使用任何其他允许读取表定义的工具。)

Then open database1, attach the other database with the command:

然后打开database1,使用以下命令附加其他数据库:

ATTACH 'database2file' AS db2;

then create the table t2, and copy the data over with:

然后创建表t2,并复制数据:

INSERT INTO t2 SELECT * FROM db2.t2;

#2


6  

Shell command:

Shell命令:

sqlite3 database1

In SQLite shell:

在SQLite shell中:

sqlite> ATTACH 'database2' AS db2;
sqlite> CREATE TABLE t1 AS SELECT * FROM db2.t2;

#3


4  

You could use the sqlite3 .dump command to pipe the dump output into the other db. It takes an optional argument with the table name.

您可以使用sqlite3 .dump命令将转储输出传递到另一个db。它采用表名的可选参数。

db1=~/mydb1.sqlite 
db2=~/mydb2.sqlite
t=t2

sqlite3 "$db2" ".dump $t" | sqlite3 "$db1"

If you have no common tables in both databases, you can leave out the table name and copy all tables.

如果两个数据库中都没有公用表,则可以省略表名并复制所有表。

If the tables are big, this may be slow because it will do INSERTs. If they are huge, and it is really too slow, maybe .import would be faster. You could try something like

如果表很大,这可能会很慢,因为它会执行INSERT。如果它们很大,而且它实在太慢了,也许。进口会更快。你可以试试像

sqlite3 "$db2" ".schema $t" | sqlite3 "$db1"
sqlite3 "$db2" "SELECT * FROM $t" | sqlite3 "$db1" ".import /dev/stdin $t"

#1


30  

Open database2 with the sqlite3 command-line tool and read the table definition with the command .schema t2. (Alternatively, use any other tool that allows to read the table definition.)

使用sqlite3命令行工具打开database2,并使用命令.schema t2读取表定义。 (或者,使用任何其他允许读取表定义的工具。)

Then open database1, attach the other database with the command:

然后打开database1,使用以下命令附加其他数据库:

ATTACH 'database2file' AS db2;

then create the table t2, and copy the data over with:

然后创建表t2,并复制数据:

INSERT INTO t2 SELECT * FROM db2.t2;

#2


6  

Shell command:

Shell命令:

sqlite3 database1

In SQLite shell:

在SQLite shell中:

sqlite> ATTACH 'database2' AS db2;
sqlite> CREATE TABLE t1 AS SELECT * FROM db2.t2;

#3


4  

You could use the sqlite3 .dump command to pipe the dump output into the other db. It takes an optional argument with the table name.

您可以使用sqlite3 .dump命令将转储输出传递到另一个db。它采用表名的可选参数。

db1=~/mydb1.sqlite 
db2=~/mydb2.sqlite
t=t2

sqlite3 "$db2" ".dump $t" | sqlite3 "$db1"

If you have no common tables in both databases, you can leave out the table name and copy all tables.

如果两个数据库中都没有公用表,则可以省略表名并复制所有表。

If the tables are big, this may be slow because it will do INSERTs. If they are huge, and it is really too slow, maybe .import would be faster. You could try something like

如果表很大,这可能会很慢,因为它会执行INSERT。如果它们很大,而且它实在太慢了,也许。进口会更快。你可以试试像

sqlite3 "$db2" ".schema $t" | sqlite3 "$db1"
sqlite3 "$db2" "SELECT * FROM $t" | sqlite3 "$db1" ".import /dev/stdin $t"