1

I try to insert string to mysql in bash, so I do the next:

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

When I do it, I get the next massage:

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.

and other commands. What I do wrong?

3
  • You're missing the database argument. Also, do you really want a space after www. and before .com? Commented Jun 10, 2013 at 16:15
  • Although when I try what you've written, the error I get is ERROR 1046 (3D000) at line 1: No database selected Commented Jun 10, 2013 at 16:16
  • @Barmar: It something with the "'$message'". When I change it to "blabla" for example, everything its OK. Commented Jun 10, 2013 at 16:19

3 Answers 3

1

Please have a try with this one:

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

At least it worked for me, when I tested it with this:

message="<a href = http://www."
message="$message hello"
message="$message .com"
mysql -u root -pwhatever -Bse "SELECT '$message'";
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

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

The problem was the spaces in $message were ending the -e option.

Comments

0
  1. Instead of piecing together the message variable like you did, this is easier to read:

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

    This is equivalent to the example in the original post, though the text itself doesn't look meaningful.

  2. You can pass your query to mysql like this:

    mysql -u root -pmypass -Bse  "INSERT INTO atTable VALUES (null, '$message')"
    
  3. If message contains single quotes, you need to escape them, you can do like this:

    message=$(echo "$message" | sed -e "s/'/\\\\'/")
    
  4. Instead of putting your root password on the command line, I recommend to put that information in the .my.cnf file of your home directory, for example:

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

    However, before entering the real password, protect the file first like this:

    touch .my.cnf
    chmod 600 .my.cnf
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.