Rich Signell
Rich Signell

Reputation: 16347

Why does this sqlite3 command work interactively but not in batch?

I'd like to create a batch script to remove all the records from my db.

If I start up the command line interface, I have no problems:

[tomcat@scsrv26v pycsw]$ sqlite3 /var/www/pycsw/tests/suites/cite/data/records.db
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> delete from records;
sqlite> .quit

but if I try to run this from batch, it doesn't work:

#!/bin/bash
sqlite3 -batch /var/www/pycsw/tests/suites/cite/data/records.db <<"EOF"
delete from records;
.quit
EOF

Here's the error I get:

Error: near line 1: no such table: records

Any idea what the problem is?

Upvotes: 0

Views: 1934

Answers (2)

tomkralidis
tomkralidis

Reputation: 101

Might be something with the path of the .db file argument (see No Such Table error). If the .db doesn't exist, it is created with as an empty db file.

To test, see if this works:

#!/bin/bash
sqlite3 -batch tests/suites/cite/data/records.db <<"EOF"
select count(*) from records;
.quit
EOF

Having said this, this worked for me:

$ sqlite3 --version
3.8.7.1 2014-10-29 13:59:56 3b7b72c4685aa5cf5e675c2c47ebec10d9704221
$ sqlite3 tests/suites/cite/data/records.db "delete from records"

Upvotes: 0

MC ND
MC ND

Reputation: 70923

It is the first time I see the indicated syntax. Sorry, I have no idea of how it works.

But this can be solved with ((i use this in windows batch files and as far as i know also in linux):

Command directly from command line

sqlite3 records.db 'delete from records'

Command piped to sqlite

echo delete from records; | sqlite3 records.db 

And, of course, you can place the commands in a file and redirect into sqlite

sqlite3 records.db < myCommands.sql

Upvotes: 1

Related Questions