简体   繁体   English

从SQLite 3导出数据

[英]Exporting data from SQLite 3

I need a simple way to export data from an SQLite database of multiple tables, then import them into another database. 我需要一种简单的方法从多个表的SQLite数据库导出数据,然后将它们导入另一个数据库。

Here is my scenario. 这是我的情景。 I have 5 tables: A, B, C, D, E. 我有5个表:A,B,C,D,E。

Each table has a primary key as the first column called ID. 每个表都有一个主键作为名为ID的第一列。 I want a Unix command that will dump ONLY the data in the row from the primary key in a format that can be imported into another database. 我想要一个Unix命令,它只能以可导入另一个数据库的格式从主键转储行中的数据。

I know I can do a 我知道我能做到

sqlite3 db .dump | grep INSERT

but that gives me ALL data in the table. 但是这给了我表中的所有数据。 I'm not a database expert, and I'm trying to do this with all unix commands in which I can write a shell script, rather than writing C++ code to do it (because that's what people are telling me that's the easiest way). 我不是数据库专家,我正在尝试使用所有unix命令来编写shell脚本,而不是编写C ++代码来执行此操作(因为这是人们告诉我这是最简单的方法) 。 I just refuse to write C++ code to accomplish a task that possible can be done in 4-5 statements from the command line. 我只是拒绝编写C ++代码来完成一项任务,可以在命令行的4-5语句中完成。

Any suggestions? 有什么建议?

This may look a little weird, however you may give it a try: 这可能看起来有点奇怪,但你可以尝试一下:

Create text file and place following statements there: 创建文本文件并在其中放置以下语句:

.mode insert
.output insert.sql
select * from TABLE where STATEMENT; -- place the needed select query here
.output stdout

Feed this file to sqlite3: 将此文件提供给sqlite3:

$ sqlite3 -init initf DATA.DB .schema > schema.sql

As the result you will get two files: one with simple "inserts" ( insert.sql ) and another with db schema ( schema.sql ). 结果您将获得两个文件:一个使用简单的“插入”( insert.sql ),另一个使用db schema( schema.sql )。

Suggest finding a tool that can take your query and export to a CSV. 建议找一个可以查询并导出为CSV的工具。 It sounds like you wanted a script. 听起来你想要一个脚本。 Did you want to reuse and automate this? 您想重用并自动化吗?

For other scenarios, perhaps consider the sqlite-manager Firefox plugin . 对于其他场景,也许可以考虑使用sqlite-manager Firefox插件 It supports running your adhoc queries, and exporting the results to a CSV. 它支持运行您的adhoc查询,并将结果导出为CSV。

Within that, give it this statement: 在其中,给它这个声明:

 SELECT ID FROM TableA

Repeat for each table as you need. 根据需要对每个表重复。

你也可以使用SQLite的quote函数。

echo "SELECT 'INSERT INTO my_new_table (my_new_key) VALUES (' || quote(my_old_key) || ');' FROM my_old_table;" | sqlite my_table > statements.sql

You can use sqlite3 bash. 你可以使用sqlite3 bash。 For example if you want to get insert query for all records in one table, you can do the followings: 例如,如果要对一个表中的所有记录进行insert查询,可以执行以下操作:

$ sqlite3 /path/to/db_name.db
>>.mode insert
>>.output insert.sql
>>select * from table_name;

It will creates a file name called insert.sql and puts insert query for every record in the given table. 它将创建一个名为insert.sql的文件名,并为给定表中的每个记录添加insert查询。

A sample for what you get in insert.sql : 您在insert.sql获得的insert.sql

INSERT INTO "table" VALUES("data for record one");
INSERT INTO "table" VALUES("data for record two");
..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM