1

我尝试在 bash 中向 mysql 插入字符串,所以我执行以下操作:

message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse 'INSERT INTO atTable VALUES (null, "'$message'")'

当我这样做时,我会得到下一个按摩:

mysql  Ver 14.14 Distrib 5.1.69, for debian-linux-gnu (i486) using readline 6.1
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
  -A, --no-auto-rehash
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect.
  -B, --batch         Don't use history file. Disable interactive behavior.
                      (Enables --silent.)
  --character-sets-dir=name
                      Directory for character set files.

和其他命令。我做错了什么?

4

3 回答 3

1

请试试这个:

message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')";

至少它对我有用,当我用这个测试它时:

message="<a href = http://www."
message="$message hello"
message="$message .com"
mysql -u root -pwhatever -Bse "SELECT '$message'";
于 2013-06-10T16:26:43.843 回答
1

尝试这个:

mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')"

问题是空格$message结束了这个-e选项。

于 2013-06-10T16:26:51.633 回答
0
  1. 而不是像你一样拼凑message变量,这更容易阅读:

    message="<a href = http://www. $d .com"
    

    这相当于原始帖子中的示例,尽管文本本身看起来没有意义。

  2. 您可以将查询传递给mysql这样的:

    mysql -u root -pmypass -Bse  "INSERT INTO atTable VALUES (null, '$message')"
    
  3. 如果message包含单引号,你需要转义它们,你可以这样做:

    message=$(echo "$message" | sed -e "s/'/\\\\'/")
    
  4. 我建议不要将您的 root 密码放在命令行上,而是将该信息放在.my.cnf您的主目录的文件中,例如:

    [client]
    database=yourdbname
    user=root
    password=yourpass
    

    但是, 输入真实密码之前,请先保护文件,如下所示:

    touch .my.cnf
    chmod 600 .my.cnf
    
于 2013-06-10T19:10:53.993 回答