如何使用 Bash 插入 SQLite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4152321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:54:02  来源:igfitidea点击:

How to INSERT INTO SQLite database using Bash?

bashscriptingsqlite

提问by ThG

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

我用 SQLite 创建了一个数据库 - tasks.db。该数据库有一个表 - 待办事项 - 具有以下字段:id(pk)、日期(现在有触发器)、项目、截止日期、状态、描述

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

要从命令行在 SQLite 中输入新行,我必须写:

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 :

这是一个相当长且容易出错的过程。所以我决定用一个 shell 脚本(bsq)“自动化”它,它运行如下:

#!/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.

......当我运行时没有任何反应:sh bsq。序列出现然后让我回到提示。

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

我哪里出错了或者我遗漏了什么(ENTER?但我该怎么做?)?

Thanks for your help.

谢谢你的帮助。

回答by khachik

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