Ticket #10869: testmysqlpp.pl

File testmysqlpp.pl, 1.6 KB (added by bluedalmatian, 10 years ago)

Simple Perl script using DBD::mysqlPP to connect to a MySQL Server and print if it succeeds. See comments in source

Line 
1#!perl
2
3# Simple test program using DBD::mysqlPP to connect to a MySQL server and print connected OK to std out if
4#it succeeds.
5#
6#You will need a MySQL database to which you have access and edit the $dsnm $dbuser & $dbpass variables
7#below accordingly
8#
9#
10
11use DBI;
12
13#MySQL parameters defined once here and used globally in each DB function
14$dsn = "DBI:mysqlPP:database=DBNAME;host=MYSQL.SERVER.DOMAIN";
15$dbuser = "DBUSERNAME";
16$dbpass = "DBPLAINTXT-PASSWD";
17
18&connectAndStart();
19print("connected OK\n");
20#run some query here
21&rollback();
22
23
24sub connectAndStart()
25{
26 $dbh= DBI->connect($dsn,$dbuser,$dbpass,{RaiseError => 1});
27 if (!defined($dbh))
28 {
29 print("connectAndStart: unable to connect to database\n");
30 exit 1;
31 }
32
33 $sth=$dbh->prepare("BEGIN");
34 if($sth->execute())
35 {
36 $sth->finish();
37 }
38 else
39 {
40 print("connectAndStart: connected but unable to BEGIN transaction\n");
41 exit 1;
42 }
43
44}
45
46sub commit()
47{
48 $sth=$dbh->prepare("COMMIT");
49 if ($sth->execute())
50 {
51 if (!($sth->finish()))
52 {
53 print("commit: unable to COMMIT transaction (A)\n");
54 exit 1;
55 }
56 if (!($dbh->disconnect()))
57 {
58 print("commit: unable to COMMIT transaction (B)\n");
59 exit 1;
60 }
61 }
62 else
63 {
64 print("commit: unable to COMMIT transaction (C)\n");
65 exit 1;
66 }
67
68}
69
70sub rollback()
71{
72 $sth=$dbh->prepare("ROLLBACK");
73 if ($sth->execute())
74 {
75 if (!($sth->finish()))
76 {
77 print("rolback: unable to ROLLBACK transaction (A)\n");
78 exit 1;
79 }
80 if (!($dbh->disconnect()))
81 {
82 print("rollback: unable to ROLLBACK transaction (B)\n");
83 exit 1;
84 }
85 }
86 else
87 {
88 print("rollback: unable to ROLLBACK transaction (C)\n");
89 exit 1;
90 }
91}