Matter
Matter

Reputation: 449

Sqlite3 multiple arguments in bash script

I'm new to sqlite3 but I'm starting to get the hang of it. I'm creating a simple command line based tool to remove entries from a database. The command I'm using works fine in interactive mode. It looks like this:

sqlite3 db.sqlite
SQLite version 3.7.12 2012-04-03 19:43:07
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT computer_name FROM machine WHERE serial_number = "C02JT0XXXXXX";
MacBook01

But when I try to put it in a shell command it doesn't work:

$ sqlite3 db.sqlite "SELECT computer_name FROM machine WHERE serial_number = "C02JT0XXXXXX";"
Error: no such column: C02JT0XXXXXX

The weird thing is when I use a different argument is DOES work:

$ sqlite3 db.sqlite "SELECT computer_name FROM machine WHERE id = "1";"
MacBook01

It looks like if the second argument is longer than 1 character it fails. I tried a few others as well and they gave me the same error.

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 995

Answers (2)

Adrian Ratnapala
Adrian Ratnapala

Reputation: 5673

In bash, your first expression becomes SELECT computer_name FROM machine WHERE serial_number = C02JT0XXXXXX; -- effectively the quotes are stripped off. This is OK for id = 1 because that is a numerical comparison. For the string "C02JT0XXXXXX" it causes problems. Try single quotes:

sqlite3 db.sqlite 'SELECT computer_name FROM machine WHERE serial_number = "C02JT0XXXXXX";'

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798496

You're already using those quotes.

sqlite3 db.sqlite 'SELECT computer_name FROM machine WHERE serial_number = "C02JT0XXXXXX";'

sqlite3 db.sqlite 'SELECT computer_name FROM machine WHERE serial_number = "'"C02JT0XXXXXX"'";'

Upvotes: 1

Related Questions