0

我编写了一个 c-shell 脚本来连接数据库。这已经很好了,我现在想调用一个 sql 脚本来读取和打印 cetrain 表中的所有值。到目前为止,这就是我的脚本的样子

#!/bin/csh 


set MYSQL=${MYSQL_HOME}/mysql
${MYSQL} ${CLEDBUSER}

其中 CLEDBUSER 设置为环境变量,如下所示 - CLEADBUSER=-uusername -ppassword -Ddatabasename

我能够运行脚本并连接到数据库。当我运行他的脚本时,它给了我等待下一个命令的 msql pront。所以我在脚本中添加了一个包含(SELECT)语句的变量来查询数据库。现在我的脚本看起来像这样

#!/bin/csh 


set MYSQL=${MYSQL_HOME}/mysql

set SELECTER="SELECT * FROM TB_EARTH_UI;"

${MYSQL} ${CLEDBUSER} ${SELECTER} 

问题是它没有返回我所有的行和columsn,但它返回了mysql promt 和默认选项中可用命令的列表以及vairables 和布尔选项。为什么我的 SELECT 语句没有被读取?

4

1 回答 1

1

MySQL client (mysql) expects SQL instructions on its standard input (e.g. your keyboard, when invoking from the shell).

You could do: [edit: please ignore, this one is off-topic]

${MYSQL} ${CLEDBUSER} < text_file_of_sql_statements.sql

or

${MYSQL} ${CLEDBUSER} << EOF
  ${SELECTER}
  # you can add other litteral SQL statements here, or more variables containing SQL statements
EOF

or

${MYSQL} ${CLEDBUSER} --execute="${SELECTER}"

[edit]

I totally misunderstood the OP's question. I didn't get it that he was trying to execute SQL statements from a variable. I have edited the above options (thank you outis). Here is another variation:

echo ${SELECTER} | ${MYSQL} ${CLEDBUSER}

Also, the --skip-column-names option could make it easier for you to parse the output.

于 2012-06-27T17:50:31.477 回答