bashscriptingsqlite

How to INSERT INTO SQLite database using Bash?


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.


Solution

  • #!/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\");"