Examples of adding, deleting, updating, querying operations for perl database
Updated: August 16, 2014 10:18:52 Submission: junjie
This article mainly introduces examples of adding, deleting, updating and querying perl database. This article directly gives the operation code. Friends who need it can refer to it.
Note: The database specified by SID is used when connecting, so the database is not specified in the connection.
#!/usr/bin/perl use strict; use warnings; use DBI; my $db_name="geneva_admin"; my $db_passwd="geneva_admin"; my $dbh=DBI->connect("dbi:Oracle:","$db_name","$db_passwd") or die "Can't connect to oracle database:$DBI::errstr\n"; my $sth=$dbh->prepare("select a,b from a_tmp where a=2") or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute or die "Can't execute:$DBI::errstr\n"; while (my @row = $sth->fetchrow_array()){ my ($a,$b) = @row; print "1..\$a=$a,\$b=$b\n"; } $sth->finish(); my $row=3; my $sql="select a,b from a_tmp where a = ?"; $sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute($row) or die "Can't execute:$DBI::errstr\n"; while (my @row = $sth->fetchrow_array()){ my ($a,$b) = @row; print "2..\$a=$a,\$b=$b\n"; } $sth->finish(); my $row_a=3; my $row_c=0; $sql="select a,b from a_tmp where a = ? and c = ?"; $sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute($row_a,$row_c) or die "Can't execute:$DBI::errstr\n"; while (my @row = $sth->fetchrow_array()){ my ($a,$b) = @row; print "3..\$a=$a,\$b=$b\n"; } $sth->finish(); for $row(1,2,3){ $sql="select a,b from a_tmp where a = ?"; $sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute($row) or die "Can't execute:$DBI::errstr\n"; while (my @row = $sth->fetchrow_array()){ my ($a,$b) = @row; print "4..\$a=$a,\$b=$b\n"; } } $sth->finish(); #for $row(1,2,3){ #$sql="insert into a_tmp # values (?,?,?)"; #$sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; #$sth->execute($row,$row+1,$row+2) or die "Can't execute:$DBI::errstr\n"; #} ##$dbh->commit; #$sth->finish(); #$sql="insert into a_tmp # values (100,30,2)"; #$sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; #$sth->execute or die "Can't execute:$DBI::errstr\n"; ##$dbh->commit; #$sth->finish(); for $row(1,2,3){ $sql="update a_tmp set b = ? , c = ? where a = ?"; $sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute($row+100,$row+50,$row) or die "Can't execute:$DBI::errstr\n"; } #$dbh->commit; $sth->finish(); for $row(1,2,3){ $sql="delete from a_tmp where c=2"; $sth=$dbh->prepare($sql) or die "Can't prepare SQl prepare:$DBI::errstr\n"; $sth->execute or die "Can't execute:$DBI::errstr\n"; } #$dbh->commit; $sth->finish(); $dbh->do("insert into a_tmp values (1,1,1)") or die "$DBI::errstr\n"; $dbh->do("delete from a_tmp where c=51") or die "$DBI::errstr\n"; #$dbh->commit; $sth->finish(); $dbh->disconnect;
Related Articles
Perl script realizes the function of detecting the host heartbeat signal
This article mainly introduces the function of Perl script to detect the host heartbeat signal. The code in this article can also be used as an example of perl serial communication. Friends who need it can refer to it.2014-10-10The difference between or in perl and ||
or lower priority than ||, except that, there is no difference between the two2013-02-02Perl split string split function usage guide
This article briefly introduces the usage of Perl split function. A very useful function in Perl is the Perl split function - splitting the string and putting the split result into an array2013-02-02Perl and shell get code for yesterday, tomorrow or several days ago
This article mainly introduces the code for obtaining the dates of perl and shell yesterday, tomorrow or several days ago. Friends who need it can refer to it2014-04-04Perl uses variables as handle introduction
In perl code, there is a special thing about open, that is, if you give it an undefined (this is why hash is used) variable as an indirect file handle, then Perl will automatically define that variable for you, that is, it will automatically activate it so that it contains a suitable file handle reference.2013-02-02python get command line parameters function
Under perl, get the command line parameters and number of functions.2009-04-04How to convert local time and UNIX timestamps in Perl
This article mainly introduces the method of converting local time and UNIX timestamps in Perl. It mainly uses the Date::Parse module in Perl. Friends who need it can refer to it.2015-06-06Perl file operation examples
This article mainly introduces several examples of perl file operation, explains deleting files, reading files, and reading them at one time. Friends who need it can refer to it2014-06-06The application of perl subroutine and the importance of private (my) declarations in subroutines
My in perl can define private variables, which can prevent errors in many cases.2013-02-02How to use perl's Tie::File module to delete file fixed lines
Use the perl's Tie::File module to delete fixed lines of file. The processing here mainly uses the perl's Tie::File module to bind arrays and files. Then you can use the perl's splice function to operate the array to achieve the purpose of operating files. Friends who are interested in deleting fixed lines of file by perl, follow the editor to take a look.2023-12-12