I'm trying to dump the schema for test.db
only (i.e. 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!
You can export the structure and contents to a . sql file that can be read by just about anything. File > Export > Database to SQL file. Technically, the SQLite shell is not SQLite but a console application linked to SQLite which only is a library.
The . dump command converts the entire structure and data of an SQLite database into a single text file. By default, the . dump command outputs the SQL statements on screen. To issue the output to a file, you use the .
The shell allows redirection, and sqlite3
can get the command as a parameter:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With