18

I have created a database - tasks.db - with SQLite. This database has one table - todo - with the following fields : id (pk), date (NOW with trigger), project, duedate, status, description

To enter a new row in SQLite from the command line, I have to write :

sqlite3 tasks.db "insert into todo (project,duedate,status,description) values (2010-11_18,'Home','Urgent','Call the plumber');"

which is a rather long and error-prone process. So I decided to "automate" it with a shell script (bsq) which runs as follows :

#!/bin/sh
echo "What project ?"
read Proj
echo "For when ?"
read Due
echo "What status ?"
read Stat
echo "What to do ?"
read Descr

echo sqlite3 tasks.db "insert into todo (project,duedate,status,description) values ('$Proj',$Due,'$Stat','$Descr');"

… and nothing happens when I run : sh bsq. The sequence appears then brings me back to the prompt.

Where did I go wrong or what did I omit (ENTER ? but how do I do that ?)?

Thanks for your help.

7
  • What do you expect? You call echo sqlite ... it won't invoke sqlite. Please format the source properly. Use the icons above the textarea. Commented Nov 11, 2010 at 7:55
  • Is the last echo correct? Or are you executing sqlite3 directly? It works fine here; echoing the command at the end. Commented Nov 11, 2010 at 7:56
  • Also: In sh '$Proj' is not treated as a variable. Use "$Proj" Commented Nov 11, 2010 at 7:57
  • @khachik; incorrect.. the single quotes are within double quotes, so they have no meaning and will be used as is. Commented Nov 11, 2010 at 7:59
  • @roe: agreed. I didn't see the double quotes. Commented Nov 11, 2010 at 8:04

2 Answers 2

35
#!/bin/sh
echo "What project ?"
read Proj
echo "For when ?"
read Due
echo "What status ?"
read Stat
echo "What to do ?"
read Descr

echo "im gonna run" sqlite3 tasks.db "insert into todo \
     (project,duedate,status,description) \
     values (\"$Proj\",$Due,\"$Stat\",\"$Descr\");"
sqlite3 tasks.db "insert into todo (project,duedate,status,description) \
         values (\"$Proj\",$Due,\"$Stat\",\"$Descr\");"
Sign up to request clarification or add additional context in comments.

4 Comments

it worked perfectly, of course. Thanks a lot (and this time, I did not forget to ACCEPT the answer...)
please note: this does not take care of escaping
What do I have to do to handle escaping?
To handle escaping don't use sqlite. That is the best answer I found so far within few posts.
1

printf, a subprocess and a pipe works for me with sqlite 3.26.0

(printf "INSERT INTO atable VALUES ('%s', '%s', '%s');" $foo $bar $baz) | sqlite3 some.db

That will probably eliminate the need to escape double quotes.

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.