ʹÓÃPerl DBI/DBDÁ¬½ÓMysql
°²×°»·¾³£º
±¾È˵çÄÔÒÑ°²×°µÄVMware,²¢ÇÒÊÂÏÈ°²×°ÁËOSºÍMysql:
OS : RedHat Linux AS4(ĬÈÏÒÑ°²×°Perl)
Mysql: 5.0.22(´ÓÍøÉÏÏÂÔØSOURCE°ü½øÐа²×°)
°²×°¹ý³Ì£º
±¾È˵çÄÔÒÑÉÏÍø£¬VMwareʹÓõÄÊÇNAT·½Ê½£¬¿ÉÒÔÓÃÕâÖÖ·½Ê½Ö±½Ó´ÓÍøÉÏ°²×°,²»Ò׳ö´í¡£
1. [root@RHEL4 DB]# perl -MCPAN -e "install DBI"
2. [root@RHEL4 DB]# perl -MCPAN -e "install DBD::mysql"
ͨ¹ýÒÔÉϹý³Ì½«perlµÄDBI¼°DBD°²×°Íê±Ï¡£
ʹÓÃDBI/DBDÁ¬mysqlÊý¾Ý¿â:
1. ÔÚMysql server½¨Á¢Ò»¸öAccount--"james"(ʹÓÃÒÔÏÂÖ¸Áî)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass';
mysql> select host,user from user;
+-----------+-------+
| host | user |
+-----------+-------+
| RHEL4 | |
| RHEL4 | james |
| RHEL4 | root |
| localhost | |
| localhost | james |
| localhost | root |
+-----------+-------+
6 rows in set (0.00 sec)
2. ʹÓÃjamesÕʺÅÁ¬Èëmysql,ÔÚtestÊý¾Ý¿âÖн¨Á¢Ò»¸ötable:
mysql> CREATE TABLE users (id INT(4) PRIMARY KEY, username VARCHAR(25),
country VARCHAR(2));
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO users VALUES (1, 'john', 'IN'), (2, 'tom', 'US'), (3,
'layla', 'US');
Query OK, 3 rows affected (0.11 sec)
Records: 3 Duplicates: 0 Warnings: 0
3. дһ¸öperl½Å±¾½øÐвéѯ
#!/usr/bin/perl
use DBI;
my $dbh=DBI->connect("DBI:mysql:database=test;host=localhost","james","james",{'RaiseError' => 1});
my $sth=$dbh->prepare("select * from users");
$sth->execute();
while(my $ref=$sth->fetchrow_hashref()){
print "ID:$ref->{'id'},username:$ref->{'username'},Country:$ref->{'country'}\n";
}
$dbh->disconnect();
×ܽá
Ïà¹ØÕ¾µã£º
http://www.chinahtml.com/programming/4/2006/cgi-11603810267057.shtml
http://fanqiang.chinaunix.net/program/perl/2006-05-31/4379.shtml
Õû¸öDBI£¬DBDµÄ¹¤×÷ģʽÈçÏ£º
[font=ºÚÌå]ÈÃÎÒÃÇ¿´¿´Á¬½ÓÊý¾Ý·¢ÉúµÄµäÐ͵Ĺý³Ì£º
1¡¢ÔØÈëDBIÇý¶¯³ÌÐò
2¡¢Ê¹ÓÃDBDÁ¬½ÓÏàÓ¦µÄÊý¾Ý¿â
3¡¢´ò¿ªº¬ÓÐSQLÖ¸ÁîµÄÓαê
4¡¢È¡»ØÊý¾Ý¼¯
5¡¢¹Ø±ÕÓαê
6¡¢¹Ø±ÕÊý¾Ý¿âÁ¬½Ó
7¡¢Í˳ö
[/font]