Revision: 5146
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 14, 2008 11:30 by eszpee
Initial Code
sub db_connect { my ($dbname, $dbuser, $dbpass) = @_; my $dbh = DBI->connect("DBI:mysql:$dbname",$dbuser,$dbpass); #$dbh->do(qq{set character set 'utf8';}); return $dbh; } sub do_sql { # Takes: $dbh, $sql # Returns: status my $dbh = shift || die "Database not connected!\n"; my $sql = shift || die "Missing SQL statement???\n"; return $dbh->do($sql); } sub execute_sql { # Takes: $dbh, $sql # Returns: $result_arrayref my $dbh = shift || die "Database not connected!\n"; my $sql = shift || die "Missing SQL statement???\n"; my $sth = $dbh->prepare($sql); $sth->execute; my $result = $sth->fetchall_arrayref({}); # {} => Return arrayref of hashrefs return $result; } sub do_insert { #takes: $dbh, $table, $datahash #returns: status my $dbh = shift || die "Database not connected!\n"; my $table = shift || die "Missing table!\n"; my $datahash = shift || die "Nothing to insert!\n"; my $insert = "INSERT INTO $table (" . join(',', keys %$datahash) . ') VALUES (' . join(',', values %$datahash) . ');'; return &do_sql($dbh, $insert); }
Initial URL
Initial Description
Initial Title
MySQL based functions
Initial Tags
mysql, sql
Initial Language
Perl