使用Shell脚本将行添加到数据库表中? [英] Add rows into Database table using Shell Script?
问题描述
如何通过Shell脚本在数据库的表中添加行?
我有以下内容:
How to add rows into table in the Database by Shell Script? I have following:
Postgres Database Name: dm
Table Name: Error Codes
Password of Database: ******
我想在表中添加行吗?
我刚刚开始执行Shell脚本...
I am just starting Shell Scripting...
我的代码:
#!/bin/sh
DATABASE="dm"
USERNAME="postgres"
HOSTNAME="HBG"
export PGPASSWORD="postgres"
psql -h $HOSTNAME -U $USERNAME $DATABaSE << EOF
select * from Error Codes
EOF
获取错误:
psql: could not connect to server: Connection refused
Is the server running on host "HBG" (192.168.0.241) and accepting
TCP/IP connections on port 5432?
推荐答案
看看如何使用 psql
命令。
Have a look on how to use the psql
command.
您可以使用此命令声明任何SQL查询。例如:
You can state any SQL query using this command. For example:
psql -c "SELECT * FROM foo" mydatabase myusername
当然,您也可以使用 INSERT
, UPDATE
和所有其他SQL命令,只要 myusername
有权这样做。
Of course you can also use INSERT
, UPDATE
and all other SQL commands, as long as myusername
has the rights to do so.
如果需要要在没有密码的情况下执行此操作(但是强烈建议您不要这样做,并且不安全),您可以在脚本的开头添加它:
If you want to do this WITHOUT a password (but this is strongly disrecommended and unsecure) you can add this at the start of your script:
set PGPASSWORD=<password>
有关完整文档,请参见此处: http://www.postgresql.org/docs/7.4/static/app-psql.html
See here for full documentation: http://www.postgresql.org/docs/7.4/static/app-psql.html
这篇关于使用Shell脚本将行添加到数据库表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!