Use shell scripts to access the mysql database

Source: Internet
Author: User
Tags mysql commands php mysql

Link: http://www.vpsee.com/2009/08/use-shell-script-to-access-mysql/

In the afternoon, I wrote a simple bash script to test the program and input a test case file,

Output the test cases and results that fail the test and save the results to the database. How can I directly access the database in a bash script?

Since mysql commands can be directly used in shell to operate databases, you can also call mysql in shell script to operate databases.

For example, use the following bash shell script to query a database:

Bash


If complex database operations are required, shell scripts are not recommended. It is very convenient to operate databases using Perl, Python, and PHP,

You can use the Perl DBI/Python MySQLdb/PHP MySQL Module Interface to operate databases.

Here is a simple example of connecting to and querying databases in these three different languages (to simplify and reduce space, delete unnecessary code ):

Perl
Use DBI;
$ Db = DBI-> connect ('dbi: mysql: test', 'vpsees ', 'Password ');
$ Query = "select * from test_mark ";
$ Cursor = $ db-> prepare ($ query );
$ Cursor-> execute;
While (@ row = $ cursor-> fetchrow_array ){
Print "@ row \ n ";
}


Import MySQLdb
Db = MySQLdb. Connect ("localhost", "vpsee", "password", "test ")
Cursor = db. cursor ()
Query = "SELECT * FROM test_mark"
Cursor.exe cute (query)
While (1 ):
Row = cursor. fetchone ()
If row = None:
Break
Print "% s, % s" % (row [0], row [1], row [2], row [3])


<? Php
$ Db = mysql_connect ("localhost", "vpsee", "password ");
Mysql_select_db ("test ");
$ Result = mysql_query ("SELECT * FROM test_mark ");
While ($ row = mysql_fetch_array ($ result )){
Print "$ row [0] $ row [1] $ row [2] $ row [3] \ n ";
}
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.