SQL 从命令行运行 Sqlite3 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21758769/
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
Running a Sqlite3 Script from Command Line
提问by rwolst
I am writing a shell script to scp a project and part of it involves transferring some important tables in my database.
我正在编写一个 shell 脚本来 scp 一个项目,其中一部分涉及传输我数据库中的一些重要表。
I have the following stored in the file SQLTableTransfer
:
我在文件中存储了以下内容SQLTableTransfer
:
.o MyTable1.sql;
.dump MyTable1;
.o MyTable2.sql;
.dump MyTable2;
.o MyTable3.sql;
.dump MyTable3;
And took a chance on
并抓住机会
$ sqlite3 SQLTableTransfer
But this just opened the Sqlite3 shell. What is the correct way to run a script like this from the command line?
但这只是打开了Sqlite3 shell。从命令行运行这样的脚本的正确方法是什么?
回答by CL.
The parameter you give to the sqlite3
program is the database file name.
您提供给sqlite3
程序的参数是数据库文件名。
To execute commands from a file, you must redirect the input to that file:
要从文件执行命令,您必须将输入重定向到该文件:
$ sqlite3 mydatabase.db < SQLTableTransfer
or tell it to read from that file:
或告诉它从该文件中读取:
$ sqlite3 mydatabase.db ".read SQLTableTransfer"
回答by Micha
You can get a list of the spatial tables as follows:
您可以获得空间表的列表,如下所示:
echo "SELECT f_table_name FROM geometry_columns;" | spatialite -noheader -silent your_db.sqlite
echo "SELECT f_table_name FROM geometry_columns;" | spatialite -noheader -silent your_db.sqlite
回答by T.Woody
For the lazy who want to just dump this into their .bashrc
:
对于只想将其转储到他们的懒惰的人.bashrc
:
### Execute an sqlite3 file on a given db
sql3-exec () {
# TODO: write a --help flag that doubles as error handling
# TODO: Ensure that is a db; Else display --help
# TODO: Ensure that is a .sql file; Else display --help
# TODO: Probably store a backup (or at least a flag)...
sqlite3 ".read "
true
}