36

I am writing a shell script to scp a project and part of it involves transferring some important tables in my database.

I have the following stored in the file SQLTableTransfer:

.o MyTable1.sql;
.dump MyTable1;

.o MyTable2.sql;
.dump MyTable2;

.o MyTable3.sql;
.dump MyTable3;

And took a chance on

$ sqlite3 SQLTableTransfer

But this just opened the Sqlite3 shell. What is the correct way to run a script like this from the command line?

3
  • stackoverflow.com/questions/10045035/… Commented Feb 13, 2014 at 15:52
  • I saw something like this, but that is executing SQL language from the command line, I need it to execute Sqlite language. Commented Feb 13, 2014 at 15:55
  • If those are commands that are entered at the sqlite3 command line, then this approach should still work. The general idea is that you're sending the file's contents to sqlite3's stdin. So instead of using keyboard input, it uses the input from this file. Did you give this a try already? Commented Feb 13, 2014 at 15:58

4 Answers 4

66

The parameter you give to the sqlite3 program is the database file name.

To execute commands from a file, you must redirect the input to that file:

$ sqlite3 mydatabase.db < SQLTableTransfer

or tell it to read from that file:

$ sqlite3 mydatabase.db ".read SQLTableTransfer"
Sign up to request clarification or add additional context in comments.

6 Comments

Ok, thanks. However for some reason when I am using .dump when doing this, it is only dumping the table PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; COMMIT;
Recently I have been trying to import a utf8 encoded sql script that contains national characters like the German äöüß and it turned out that the second method is the only way that works. Every other solution imports just question marks (I needed it for a PowerShell script).
@t3chb0t This question is not about Windows.
It doesn't matter :) it has over 2k views so I guess generally people interested in importing sql scripts in sqlite3 are coming by (like me) and even though not directly related to windows this solution worked for me. I think it's important to mention the utf8 connection because it wouldn't work otherwise on some systems (eg Windows). I've googled for an hour before I found this.
@GuilhermeSalomé How to use variables in the shell would be a different question.
|
5

For Windows CLI, assuming your database is loaded:

sqlite> .read C:\\somesubdir\\some.sql

Comments

4

For the lazy who want to just dump this into their .bashrc:

### Execute an sqlite3 file on a given db
sql3-exec () {
  # TODO: write a  --help flag that doubles as error handling
  # TODO: Ensure that $1 is a db; Else display --help
  # TODO: Ensure that $2 is a .sql file; Else display --help
  # TODO: Probably store a backup (or at least a flag)...
  sqlite3 $1 ".read $2"
  true
}

Comments

3

You can get a list of the spatial tables as follows:

echo "SELECT f_table_name FROM geometry_columns;" | spatialite -noheader -silent your_db.sqlite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.