-2

I am new to MySQL and I would like to insert into table something from bash script. Found some ways from google and it works for simple commands select * from foo but it wont work for my insert.

#!/bin/bash

qry="INSERT INTO  `video`.`asdf` (`DB_ID` , `LNX_ID` , `STIME` , `ETIME` , `TEXT` ) VALUES ( NULL , '5', '00:00:41.900', '00:00:49.600', 'Na leteckej v23423.');"

mysql --host=localhost --database 'video' --user=user --password=password << eof
$qry
eof

Thanks a lot.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
Tommy
  • 159
  • 1
  • 11

1 Answers1

1

Without seeing an error message, immediate asssumption is that bash is attempting to execute commands inside backticks, since you have double-quoted the string assigned to qry, and finding no command video, for example, it prints an error.

$ echo "This will show the date if I do `date` in backticks"
> This will show the date if I do Sat Jun 29 22:09:59 CDT 2013 in backticks

Escape the backticks with backslashes inside the double-quoted string to prevent bash from attempting to evaluate them.

qry="INSERT INTO  \`video\`.\`asdf\` (\`DB_ID\` , \`LNX_ID\` , \`STIME\` , \`ETIME\` , \`TEXT\` ) VALUES (    NULL , '5', '00:00:41.900', '00:00:49.600', 'Na leteckej v23423.');"

None of your column and table names are reserved keywords, however, and therefore don't need to be quoted at all.

qry="INSERT INTO  video.asdf (DB_ID , LNX_ID , STIME , ETIME , TEXT) VALUES (NULL , '5', '00:00:41.900', '00:00:49.600', 'Na leteckej v23423.');"

The other alternative is to use single quotes on the outer string, but that will necessitate very clumsy quoting to maintain single quotes on your inserted column values, as described here

Community
  • 1
  • 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390