SQL 将 SQLite3 转储导入回数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/311691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:20:15  来源:igfitidea点击:

Importing a SQLite3 dump back into the database

sqldatabasesqliteimport

提问by BushyMark

I feel like this is a stupid question because it seems like common sense . . . but no google search I can put together seems to be able to give me the answer!

我觉得这是一个愚蠢的问题,因为它似乎是常识。. . 但没有谷歌搜索我可以放在一起似乎能够给我答案!

I know how to get data OUT of a sqlite3 database using the .dump command. But now that I have this ASCII file titled export.sqlite3.sql . . . I can't seem to get it back INTO the database I want.

我知道如何使用 .dump 命令从 sqlite3 数据库中获取数据。但是现在我有了这个名为 export.sqlite3.sql 的 ASCII 文件。. . 我似乎无法将它恢复到我想要的数据库中。

My goal was to transfer the data I had in one rails app to another so I didn't have to take all sorts of time creating dummy data again . . . so I dumped the data from my first app, got rid of all the CREATE TABLE statements, and made sure my schema on my second app matches . . . now I just have to get it IN there.

我的目标是将我在一个 rails 应用程序中拥有的数据传输到另一个应用程序,这样我就不必再花各种时间创建虚拟数据。. . 所以我从我的第一个应用程序中转储了数据,去掉了所有的 CREATE TABLE 语句,并确保我的第二个应用程序上的架构匹配 . . . 现在我只需要把它放在那里。

Would anyone mind helping me out? And when you find a way, will you tell me what you plugged into the google, 'cause I am beating my head open with a spoon right now over what I thought would be an easy find.

有人介意帮我吗?当你找到一种方法时,你能告诉我你在谷歌中插入了什么,因为我现在正在用勺子敲我的头,因为我认为这很容易找到。

回答by Noah

You didn't specify your operating system and while

您没有指定您的操作系统,而

sqlite3 my_database.sqlite < export.sqlite3.sql

will work for the unix flavors, it will not work for windows.

将适用于 unix 风格,它不适用于 Windows。

The inverse of the .dump command is the .read command. The syntax would be

.dump 命令的逆命令是 .read 命令。语法是

sqlite3> .read export.sqlite3.sql

回答by James A. Rosen

This should also work:

这也应该有效:

echo '.read export.sqlite3.sql' | sqlite3 my_database.sqlite3

One possible advantage over "sqlite3 my_database.sqlite3 < export.sqlite3.sql" is that SQLite's .readcommand might (now or in the future) be more advanced than just "read in all the text and execute it." It might do batching, which would reduce memory usage for large dumps. I admit, though, that this is a pretty obscure and unlikely advantage. In all likelihood, .readsimply reads each line from the input and executes it, just like the redirection and pipe operators.

与 " sqlite3 my_database.sqlite3 < export.sqlite3.sql"相比,一个可能的优势是 SQLite 的.read命令可能(现在或将来)比“读入所有文本并执行它”更高级。它可能会进行批处理,这将减少大型转储的内存使用量。不过,我承认,这是一个非常模糊且不太可能的优势。很可能,.read只需从输入中读取每一行并执行它,就像重定向和管道运算符一样。

回答by Java Man

Use this for unix flavors.

将此用于 unix 风格。

press Ctrl+alt+T and write

按 Ctrl+alt+T 并写入

sqlite3 /home/ubuntu/output.sqlite < /home/ubuntu/input.sql

It restore your database from input file.

它从输入文件恢复您的数据库。