Operations on the database in Perl scripts

Source: Internet
Author: User

In Perl, the DBI module is an interface for MySQL database-related operations, and the DBI module needs to be installed in the environment first. The approximate steps for Perl to handle database operations are as follows:
#声明使用DBI模块
Use DBI;
#设置数据库连接参数, specify the connection database name, the server IP address where the database resides, the connection user name, the password
# db_name is the name of the database to be connected, IP is the server IP address of the database
My $database = ' dbi:mysql:database=db_name;host=ip ';
My $user = ' user_name ';
My $pw = ' password ';
#连接数据库
My $dbh =dbi->connect ($database, $user, $PW, {' RaiseError ' =>1}) | | Die "can ' t connect to the database: $DBI:: errstr\n";

# If you need to support Chinese, execute the following statement. To ensure that Chinese data is stored in the database using UTF8 encoding. Also note that when using the database client to use UFT8 encoding to view the data, otherwise the data can be found garbled.
$DBH->do ("Set names UTF8");

Several data operations are listed below:
Total Statistics Records:
# Key_word is the primary key for the table, table_name
My $sql 0= "select count (Key_word) from table_name";
My $sth 0= $dbh->prepare ("$sql 0");
$sth 0->execute ();
#读取一行数据 and put the data into the array.
My $ref 0= $sth 0->fetchrow_array ();
Print "$ref 0\n";
$sth 0->finish ();

Cycle through queries and update records for a table
# Querying records from a table
My $sql = "SELECT * FROM table_name";
My $sth = $dbh->prepare ("$sql");
$sth->execute ();
# $sth->fetchrow_hashref () takes the next line as a reference to a hash table. This method takes a row of data and returns a reference to a hash table that contains the field name/value pairs.
while (my $ref = $sth->fetchrow_hashref ())
{
# Print record field, ID primary key
Print "$ref->{' id '} $ref->{' Segment1 '} $ref->{' Segment2 '}\n";
#更新记录
My $update _sql= "UPDATE table_name set segment1=value1 segment2=value2 where id= $ref->{' id '}";
My $update _sth= $dbh->prepare ("$update _sql");
$update _sth->execute ();
$update _sth->finish ();
}
# Loop end, end operation.
$sth->finish ();

#所有数据库操作结束, disconnect the database.
$DBH->disconnect ();

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.