简体   繁体   中英

sqlite3: dump schema into .sql file from command line

I'm trying to dump the schema for test.db only (ie no data) into a file called schema.sql from the command line in OS X without launching sqlite3.

I know I can do:

sqlite3
.open test.db
.output schema.sql
.schema
.quit

But I don't want to launch sqlite 3. This...

echo '.output schema.sql' | sqlite3 test.db

creates the empty file but this...

echo '.schema' | sqlite3 test.db

only prints the schema. How can I write it to that file from Terminal?

Thanks!

shell允许重定向, sqlite3可以将命令作为参数获取:

sqlite3 test.db .schema > schema.sql

Figured it out! I just needed to escape the text in the echo statement:

echo -e '.output schema.sql\n.schema' | sqlite3 test.db

FYI you could also have done

( echo .output schema.sql ; echo .schema ) | sqlite3 test.db

This is running two echo commands in a subshell and piping its output.

Or

sqlite3 test.db <<EOF
.output schema.sql
.schema
EOF

See How does "cat << EOF" work in bash? for what this is doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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