我可以在 SQLite 命令行上运行脚本吗? [英] May I run a script on SQLite command line?

查看:31
本文介绍了我可以在 SQLite 命令行上运行脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以运行一个包含 SQL 语句和 .SQLite 命令?

May I run a script with SQL statements and . SQLite commands?

推荐答案

将 SQL 语句与 SQLite .commands 混合使用可能有点棘手:

Mixing SQL statements with SQLite .commands can be a bit tricky:

$ sqlite3 test.db 'create table X(x integer); .dump'
Error: near ".": syntax error

解决这个问题的最简单和最通用的方法可能是将所有命令提供给 SQLite3 命令行 shell 的标准输入,并在键入时用换行符分隔:

The easiest and most universal way to deal with this is probably to provide all commands to the standard input of the SQLite3 command line shell, separated by newlines as you would type them:

$ sqlite3 test.db <<EOF
> CREATE TABLE TEST(X INTEGER);
> .dump
> EOF
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE TEST(X INTEGER);
COMMIT;

由于 here 文档 并非在所有 shell 中都可用,您可以使用中间文件绕过此限制:

Since here documents are not available in all shells, you could use an intermediate file to bypass this limitation:

$ cat test.sql
CREATE TABLE TEST(X INTEGER);
.dump
$ sqlite3 test.db <test.sql
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE TEST(X INTEGER);
COMMIT;

这篇关于我可以在 SQLite 命令行上运行脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆