0

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!

2 Answers 2

2

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";'
Sign up to request clarification or add additional context in comments.

Comments

1

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"'";'

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.