Using a python MySQL database to operate a MySQL database using Perl
Source: Internet
Author: User
First download mysqldb
# Encoding = GBK
Import mysqldb
# Import sys
#
# Reload (sys)
# SYS. setdefaultencoding ('utf-8 ')
Print 'Connection ...'
Host = '1970. 168.1.77'
User = 'root'
Passwd = '000000'
DB = 'test'
Conn = mysqldb. Connect (host, user, passwd, DB, charset = 'gbk ')
Print 'Connection success'
Cursor = conn. cursor ()
# Query = "insert into test (ID, name) values (% s, % s )"
# Param = ("1", "Chinese character ")
Export cursor.exe cute (query, Param)
#
# Conn. Commit ()
Cursor.exe cute ('select * From test ')
Rows = cursor. fetchall ()
For row in rows:
Print row [1]
Cursor. Close ()
Conn. Close ()
Detailed methods for operating MySQL database online using Perl
1. Install the DBI Module
Step 1:
Download dbi.zip from the toolsbar and use WinZip to unbind it to a temp directory. There are three files in total:
Readme
DBI. PPD
Dbi.tar.gz
Step 2:
In the DOS window, run the following doscommand In the temp directory:
PPM install DBI. PPD
If an invalid command is prompted, run it in the PERL/bin directory.
2. Install the DBD-mysql Module
Download dbd-mysql.zip from the software. The installation method is the same.
3. Prepare the database
Start MySQL, first create a database mydata, and then create a table address
Mysql> Create Database mydata;
Query OK, 1 row affected (0.00 Sec)
Mysql> Use mydata;
Database changed
Mysql> Create Table address (
-> ID int (5) not null,
-> Name varchar (40) not null,
-> Email varchar (50) not null,
-> Telephone int (12) null );
Query OK, 0 rows affected (0.05 Sec)
Enter some data:
Mysql> insert into address values (
-> 1, 'ghthawk', 'ghthawk @ 163.net', 92384092 );
Query OK, 1 row affected (0.00 Sec)
4. Use Perl Program Insert several records and perform query.
Use dBi;
# Connecting to the database mydata
My $ DBH = DBI-> connect ('dbi: mysql: mydata') or die "cannot connect to the database:". DBI-> errstr;
Print "insert several records \ n ";
My $ something = $ DBH-> prepare (q {
Insert into address (ID, name, email, telephone) values (?, ?, ?, ?)
})});
Print "input record, press enter to finish :";
While ($ inputdata = <> ){
Chop $ inputdata;
Last unless ($ inputdata );
My ($ id, $ name, $ email, $ Tel) = Split (/,/, $ inputdata );
$ Something-> execute ($ id, $ name, $ email, $ Tel)
}
# $ DBH-> commit;
Print "the email address and phone number \ n are printed based on the input name ";
My $ something = $ DBH-> prepare ('select * From address where name =? ')
Or die $ DBH-> errstr;
Print "enter your name and press enter to finish :";
While ($ inputname = <> ){
My @ data;
Chomp $ inputname;
Last unless ($ inputname );
$ Something-> execute ($ inputname) or die "error:". $ something-> errstr;
While (@ DATA = $ something-> fetchrow_array ()){
Print "Email: $ data [2] \ t telephone: $ data [3] \ n ";
}
}
# Disconnect
$ DBH-> disconnect;
Nighthawk
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