本文目录一览:
- 1、windows下 perl 连接 mysql
- 2、如何使用perl 5.18 连接mysql 数据库
- 3、通过dbi使用perl连接mysql数据库的方法
- 4、perl连接mysql
- 5、perl环境下安装mysql的数据库驱动的问题
- 6、Perl使用MySQL数据库的语法是什么?
windows下 perl 连接 mysql
先在CMD方式下运行:
安装DBI:mysql
在MYSQL配置正确的情况下就可以开始写了,以下是例子:
#!/usr/bin/perl
use DBI;
$user="root";
$password="mscnyhlplayon";
$database="flash"; #数据库
$telbase="game_flash"; #表
my $dbh = DBI-connect("DBI:mysql:$database", $user, $password) or die "无法连接数据库: " . DBI-errstr;
$sth=MySQL_Query("select * from game_flash where id = 46");
$row = $sth-fetchrow_arrayref();
$test_txt= $row-[4];
#断开连接
$dbh-disconnect;
print "$sthbrbr$test_txtbrbr$row";
print "/BODY/HTML";
sub MySQL_Query {
my ($class,$command)=@_;
$sth=$dbh-prepare("$class") || die Lost($class,$dbh-errstr);
$sth-execute() || die Lost($class,$dbh-errstr);
return($sth);
}
sub Lost {
my ($errsA,$errsB)=@_;
print "$errsAbrbr$errsB";
print "/BODY/HTML";
exit;
}
如何使用perl 5.18 连接mysql 数据库
用DBI就可以给你段代码:
use DBI;
my $dbname = "test";
my $location = "192.168.1.115";
my $port = "3306"; #这是mysql的缺省
my $database = "DBI:mysql:$dbname:$location:$port";
my $db_user = "root";
my $db_pass = "123456";
my $dbh = DBI-connect($database,$db_user,$db_pass);
my $sql = "SELECT * FROM tablename";
my $sth = $dbh-prepare($sql);#准备
$sth-execute() or die "无法执行SQL语句:$dbh-errstr"; #执行
while (@data = $sth-fetchrow_array()) {#fetchrow_array返回row
print "Email:$data[0]t Telephone:$data[1]n";
}
$sth-finish();
$dbh-disconnect;#断开数据库连接
通过dbi使用perl连接mysql数据库的方法
通过使用DBI,用Perl可以很容易的连接到mysql数据库:
代码如下:
复制代码
代码如下:
#!/bin/perl
use
DBI;
#
Connect
to
target
DB
my
$dbh
=
DBI-connect("DBI:mysql:database=eygle;host=localhost","username","password",
{'RaiseError'
=
1});
#
Insert
one
row
my
$rows
=
$dbh-do("INSERT
INTO
test
(id,
name)
VALUES
(1,
'eygle')");
#
query
my
$sqr
=
$dbh-prepare("SELECT
name
FROM
test");
$sqr-execute();
while(my
$ref
=
$sqr-fetchrow_hashref())
{
"$ref-{'name'}n";
}
$dbh-disconnect();
perl连接mysql
下载以后需要安装,安装方法可以参考文章:
安装好了就可以使用了,例子程序如下:
#!/usr/bin/perl
use DBI;
my $dbname = "db_name";
my $location = "localhost";
my $port = "3306"; #这是mysql的缺省
my $database = "DBI:mysql:$dbname:$location:$port";
my $db_user = "root";
my $db_pass = "db_password";
my $dbh = DBI-connect($database,$db_user,$db_pass);
my $sql = "SELECT first_name,last_name FROM $table ORDER BY first_name";
my $sth = $dbh-prepare($sql);
#结果保存在$sth中
$sth-execute() or die "无法执行SQL语句:$dbh-errstr";
$dbh-disconnect;#断开数据库连接
补充:
拷贝文件到SYSTEM32不是最好的办法,其实在环境变量PATH里面加入MYSQL的DLL文件目录即可。
perl环境下安装mysql的数据库驱动的问题
用ppm装啊。。。不行的话下载到本地然后路径指为本地再装。。
或者就这样
use dbi
$self-{DBREF}=DBI-connect("DBI:mysql:database=$database;host=$host",$user,$password);
Perl使用MySQL数据库的语法是什么?
注意看下面“参考资料”中给你的链接。
use DBI;
$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
$dbh = DBI-connect($dsn, $user, $password);
$drh = DBI-install_driver("mysql");
@databases = DBI-data_sources("mysql");
or
@databases = DBI-data_sources("mysql",
{"host" = $host, "port" = $port, "user" = $user, password = $pass});
$sth = $dbh-prepare("SELECT * FROM foo WHERE bla");
or
$sth = $dbh-prepare("LISTFIELDS $table");
or
$sth = $dbh-prepare("LISTINDEX $table $index");
$sth-execute;
$numRows = $sth-rows;
$numFields = $sth-{'NUM_OF_FIELDS'};
$sth-finish;
$rc = $drh-func('createdb', $database, $host, $user, $password, 'admin');
$rc = $drh-func('dropdb', $database, $host, $user, $password, 'admin');
$rc = $drh-func('shutdown', $host, $user, $password, 'admin');
$rc = $drh-func('reload', $host, $user, $password, 'admin');
$rc = $dbh-func('createdb', $database, 'admin');
$rc = $dbh-func('dropdb', $database, 'admin');
$rc = $dbh-func('shutdown', 'admin');
$rc = $dbh-func('reload', 'admin');