-
Notifications
You must be signed in to change notification settings - Fork 17
/
AutoCommit.pl
executable file
·58 lines (42 loc) · 960 Bytes
/
AutoCommit.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use DBI;
my $db = 'AutoCommit.db';
Getopt::Long::GetOptions(
"new-db" => \my $new_db,
"autocommit" => \my $autocommit,
"no-autocommit" => \my $no_autocommit,
"commit-when-finished" => \my $commit_when_finished,
) or die;
unless (-e $db) {
$new_db = 1;
}
if ($new_db) {
if (-e $db) {
unlink $db or die;
}
}
my $dbh = DBI->connect("dbi:SQLite:dbname=$db") or die "Could not create $db";
if ($new_db) {
$dbh -> do('create table t (v)') or die;
}
else {
print "Values found in table\n ";
my @v = @{$dbh->selectall_arrayref('select v from t')};
print join "\n ", map {$_->[0]} @v;
}
if ($autocommit) {
$dbh->{AutoCommit} = 1;
}
if ($no_autocommit) {
$dbh->{AutoCommit} = 0;
}
my $sth = $dbh -> prepare('insert into t values (?)');
for (@ARGV) {
$sth -> execute($_)
}
if ($commit_when_finished) {
$sth -> commit;
}