patternsqlModerate
Scripting SQLite with dot commands
Viewed 0 times
scriptingwithcommandssqlitedot
Problem
Is it possible to write scripts that contain SQLite dot commands ( vis.
I'm building and repeatedly rebuilding an SQLite database and need to type in roughly twenty four dot command statements every time I rebuild the database. It would be really nice if I could put those commands in a script and have SQLite read them.
Is there a way to put dot commands ( not SQL statements ) into a script and have SQLite run them?
I'm on Mac OS X using bash.
.read file.sql; .separator ,; .import file.csv; )?I'm building and repeatedly rebuilding an SQLite database and need to type in roughly twenty four dot command statements every time I rebuild the database. It would be really nice if I could put those commands in a script and have SQLite read them.
Is there a way to put dot commands ( not SQL statements ) into a script and have SQLite run them?
I'm on Mac OS X using bash.
Solution
I searched the Internet for an answer last night and found nothing. Of course, today after posting this question I try again--and find an adequate response almost immediately.
The gist of it? Put your commands in a text file and direct it to SQLite using the input file descriptor, or just script everything in a bash script.
First Method:
Second Method:
And then on the command line:
The gist of it? Put your commands in a text file and direct it to SQLite using the input file descriptor, or just script everything in a bash script.
First Method:
sqlite3 database.db < commands.txtSecond Method:
#!/bin/bash --
sqlite3 -batch $1 );
.separator "\t"
.import logfile.log log_entry
EOFAnd then on the command line:
import.sh database.dbCode Snippets
#!/bin/bash --
sqlite3 -batch $1 <<"EOF"
CREATE TABLE log_entry ( <snip> );
.separator "\t"
.import logfile.log log_entry
EOFimport.sh database.dbContext
StackExchange Database Administrators Q#175986, answer score: 10
Revisions (0)
No revisions yet.