简体   繁体   English

从命令行运行 Sqlite3 脚本

[英]Running a Sqlite3 Script from Command Line

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.但这只是打开了Sqlite3 shell。 What is the correct way to run a script like this from the command line?从命令行运行这样的脚本的正确方法是什么?

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"

您可以获得空间表的列表,如下所示:

echo "SELECT f_table_name FROM geometry_columns;" | spatialite -noheader -silent your_db.sqlite

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 $1 is a db; Else display --help
  # TODO: Ensure that $2 is a .sql file; Else display --help
  # TODO: Probably store a backup (or at least a flag)...
  sqlite3 $1 ".read $2"
  true
}

对于 Windows CLI,假设您的数据库已加载:

sqlite> .read C:\\somesubdir\\some.sql

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

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