Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3: dump schema into .sql file from command line

Tags:

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!

like image 935
Joe Flip Avatar asked Aug 08 '16 15:08

Joe Flip


People also ask

How do I export SQLite database to SQL?

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.

What is SQLite dump?

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 .


2 Answers

The shell allows redirection, and sqlite3 can get the command as a parameter:

sqlite3 test.db .schema > schema.sql 
like image 144
CL. Avatar answered Oct 07 '22 16:10

CL.


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

echo -e '.output schema.sql\n.schema' | sqlite3 test.db 
like image 26
Joe Flip Avatar answered Oct 07 '22 16:10

Joe Flip