Joh*_*ohn 5 mysql command-line scripting
我正在尝试使用 bash 脚本从 MySQL 获取所有列。我希望它们存储在一个数组中。如果我这样写:
mysql -uroot -pPassword1 "select column_name
from information_schema.columns
where table_schema = 'dbName'
and table_name = 'tableName';"
Run Code Online (Sandbox Code Playgroud)
...我收到标识符名称太长的错误。如果我这样写:
mysql -uroot -pPassword1 <<- SMTH
select column_name
from information_schema.columns
where table_schema = 'dbName'
and table_name = 'tableName';
SMTH
Run Code Online (Sandbox Code Playgroud)
一切正常。但我无法将其输出到变量。有什么建议?
添加-e
选项:
\n\n\n\xc2\xb7 --execute=语句,-e 语句
\n\n执行语句并退出。默认输出格式与使用 --batch 生成的格式类似。有关一些示例,请参见第 4.2.4 节 \xe2\x80\x9c 在命令行上使用选项\n\n\n\n 行\xe2\x80\x9d。使用此选项,mysql 不使用历史文件。
\n
mysql -uroot -pPassword1 -e "select column_name from information_schema.columns where table_schema = \'dbName\' and table_name = \'tableName\';"\n
Run Code Online (Sandbox Code Playgroud)\n\n测试:
\n\nroot@onare:/home/onare# mysql -uroot -ponaare -e "select column_name from information_schema.columns where table_schema = \'test\' LIMIT 10;"\n+--------------+\n| column_name |\n+--------------+\n| AnswerID |\n| QuestionID |\n| ReplyContent |\n| id |\n| emp_id |\n| call_start |\n| call_end |\n| call_type |\n| idEmployee |\n| eName |\n+--------------+\nroot@onare:/home/onare# \n
Run Code Online (Sandbox Code Playgroud)\n\n编辑2:
\n\n如果您想避免查询标头 ( column_name
),请使用--skip-column-name
:
root@onare:/home/onare# mysql -uroot -ponaare -e "select column_name from information_schema.columns where table_schema = \'test\' LIMIT 10;" --skip-column-names\n+--------------+\n| AnswerID |\n| QuestionID |\n| ReplyContent |\n| id |\n| emp_id |\n| call_start |\n| call_end |\n| call_type |\n| idEmployee |\n| eName |\n+--------------+\nroot@onare:/home/onare# \n
Run Code Online (Sandbox Code Playgroud)\n