Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 347
ThG Avatar asked Nov 11 '10 07:11

ThG


People also ask

How do I use SQLite in terminal?

Start the sqlite3 program by typing "sqlite3" at the command prompt, optionally followed by the name the file that holds the SQLite database (or ZIP archive). If the named file does not exist, a new database file with the given name will be created automatically.

How connect to SQLite in Linux?

RazorSQL can create a new SQLite database by going to the Connections -> Add Connection Profile menu option, selecting SQLite as the database type, and then on the next screen, entering the location for the new SQLite database file, etc. You can also connect to existing SQLite databases using RazorSQL.

How do I create a SQLite database in terminal?

SQLite CREATE Database In this SQLite tutorial, here is how you can create a new database: Open the Windows Command Line tool (cmd.exe) from the start, type “cmd” and open it. From the Installation and packages tutorial, you should now have created an SQLite folder in the “C” directory and copied the sqlite3.exe on it.


1 Answers

#!/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\");"
like image 129
khachik Avatar answered Sep 19 '22 13:09

khachik