如何将 .sql 文件导入 SQLite 3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2049109/
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
How do I import .sql files into SQLite 3?
提问by webminal.org
I have .sql files which have the following content:
我有 .sql 文件,其中包含以下内容:
#cat db.sql
create table server(name varchar(50),ipaddress varchar(15),id init)
create table client(name varchar(50),ipaddress varchar(15),id init)
How do I import this file into SQLite so that these are created automatically?
如何将此文件导入 SQLite 以便自动创建这些文件?
回答by Dominic Rodger
From a sqlite prompt:
从 sqlite 提示:
sqlite> .read db.sql
Or:
或者:
cat db.sql | sqlite3 database.db
Also, your SQL is invalid - you need ;
on the end of your statements:
此外,您的 SQL 无效 - 您需要;
在语句的末尾:
create table server(name varchar(50),ipaddress varchar(15),id init);
create table client(name varchar(50),ipaddress varchar(15),id init);
回答by Eifion
Use sqlite3 database.sqlite3 < db.sql
. You'll need to make sure that your files contain valid SQL for SQLite.
使用sqlite3 database.sqlite3 < db.sql
. 您需要确保您的文件包含适用于 SQLite 的有效 SQL。
回答by eureka
Alternatively, you can do this from a Windows commandline prompt/batch file:
或者,您可以从 Windows 命令行提示符/批处理文件执行此操作:
sqlite3.exe DB.db ".read db.sql"
Where DB.dbis the database file, and db.sqlis the SQL file to run/import.
其中DB.db是数据库文件,db.sql是要运行/导入的 SQL 文件。
回答by Ian Newland
You can also do:
你也可以这样做:
sqlite3 database.db -init dump.sql