By venkate
Could someone share how to read a set of strings from terminal and insert them into MySQL in Ubuntu.
Kindly share a sample of code because i can be able to understand better.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
If you are using shell scripting you can use the -e flag for the mysql command to directly run your query. So for instance if you wanted to grab the last line of your apache error log file and insert it into the field logline in a mysql table called errorlog on a database called logs you could use the following:
#!/bin/bash
# use tail to grab the last line of your log and assign it to the variable $lline
lline=`tail -1 /var/log/apache2/error`;
# run a mysql query to insert the value of $lline sending user (root) and password (mypass)
mysql -uroot -pmypass logs -e "INSERT INTO errorlog (logline) VALUES ('$lline')";
This is a very basic example and generally I would recommend doing some validation on the value before you insert it into your database and add some error handling but this should give you the general idea.
If you are using another language like perl or php you can use backticks around your command to execute it and return the result and then use the tools provided by the language you choose to handle your interaction with mysql.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.