Inserting data into mysql database from bash script?

Posted on 2010-01-04
Last Modified: 2013-12-26
I have a textfile, a portion of the data is going into mysql.
I could write a separate program to handle this and run it from the shell script, but it would be nice if I could get it to work straight from bash. (If only to learn more bash)
I'm way of my bash league here, but I imagine something like
`mysql -h host -u user -p pass < insert into x (foo, bar) values ("baz", "$(script to get data)") `
The data I'm looking for will have ------- both prepended and appended.
Question by:letharion
    LVL 48

    Accepted Solution

    You cpuld try the following and make sure the script escapes the values suitable for the insert (e.g ' is not returned by the script.
    echo "insert into x (foo, bar) values ('baz', '"$(script to get data)"');" | mysql -h host -u user --password=pass

    Open in new window

    LVL 6

    Author Comment

    This seems to work pretty well :)

    echo 'use PPS; start transaction; insert into ProgramTestTasks (project, run, function_code) values ("2230", "1060LC", "'"$SYSTEM"'"); select max(pkey) from ProgramTestTasks; commit;' > tmpQuery
    cat tmpQuery
    $Res=$(mysql -h -u freas --password=freas < tmpQuery)
    echo "$Res"

    Copy pasting to mysql the exact command that cat prints works great, but  for some reason echo $RES doesn't return anything useful, but rather
    line 44: =max(pkey): command not found
    As if it's expanding $RES to run it's content as commands.
    I randomly tried using `` instead of $(..) but that didn't make any difference.
    LVL 6

    Author Comment

    won't work. Doh
    LVL 9

    Expert Comment

    Would you need quotes around it?
    LVL 6

    Author Comment

    gtkfreak: Sorry, not sure what you mean?
    Anyway, I got this working with hernst42's solution.
    My mistake was to try assigning to $res, and assignment in bash should leave out the $

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now