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 ";
}
?>