Perl + MySQL

  サンプルコード



  
#!/usr/local/bin/perl

# **********************************************************
# インストールは
# ppm より
# install DBI
# install DBD-MySQL
# **********************************************************


require "cgi-lib.pl";

ReadParse();
print PrintHeader();
print HtmlTop();

# **********************************************************
#	use Msql;
#	$dbh = Msql->connect($host);
#	$dbh = Msql->connect($host, $database);
#	    or
#	use Mysql;
#	$dbh = Mysql->connect(undef, $database, $user, $password);
#	$dbh = Mysql->connect($host, $database, $user, $password);
#	    or
#	$dbh = Msql1->connect($host);
#	$dbh = Msql1->connect($host, $database);
#	$dbh->selectdb($database);
#	@arr = $dbh->listdbs;
#	@arr = $dbh->listtables;
#	$quoted_string = $dbh->quote($unquoted_string);
#	$error_message = $dbh->errmsg;
#	$error_number = $dbh->errno;   # MySQL only
#	$sth = $dbh->listfields($table);
#	$sth = $dbh->query($sql_statement);
#	@arr = $sth->fetchrow;        # Array context
#	$firstcol = $sth->fetchrow;   # Scalar context
#	@arr = $sth->fetchcol($col_number);
#	%hash = $sth->fetchhash;
#	$sth->dataseek($row_number);
#	$sth->as_string;
#	@indices = $sth->listindices                   # only in mSQL 2.0
#	@arr = $dbh->listindex($table,$index)          # only in mSQL 2.0
#	($step,$value) = $dbh->getsequenceinfo($table) # only in mSQL 2.0
#	$rc = $dbh->shutdown();
#	$rc = $dbh->createdb($database);
#	$rc = $dbh->dropdb($database);
# **********************************************************
	use Mysql;

	$dbh = Mysql->connect("localhost", "test", "root", "");
	$sth = $dbh->query("select * from t_マスタ");

	while (@a = $sth->fetchrow) {
		print "$a[0]/$a[1]/$a[3]/$a[4]/$a[5]<BR>\n";
	}

# use Mysql では接続解除は必要無い(自動的に行う)

	print "<HR>";

# **********************************************************
#	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});
#	$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');
# 
# **********************************************************
	use DBI;

	$db = DBI->connect('DBI:mysql:test:localhost', 'root', '');
	$sth = $db->prepare("select * from t_マスタ");
	$sth->execute;

	while (@a = $sth->fetchrow_array) {
		print "$a[0]/$a[1]/$a[3]/$a[4]/$a[5]<BR>\n";
	}

	$sth->finish;
	$db->disconnect;


print HtmlBot();
  












   SQLの窓    create:2003/12/13  update:2014/09/07   管理者用(要ログイン)





フリーフォントWEBサービス

SQLの窓WEBサービス

SQLの窓フリーソフト

写真素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ