SQL database: Using SQLite

  1. Installing from CPAN
  2. SQL database
  3. Creating tables

Go ahead and remove that “$dbh = …” line that we added for testing. Leave “use DBI;” there.

What we would like to do is import this data from its file into a database. First, add the switch:

} elsif ($switch eq "import") {
if ($importDB = shift) {
$importHandle = DBI->connect("dbi:SQLite:dbname=$importDB");
if (!$importHandle) {
print "Unable to open database $importDB: $!\n";
}
} else {
help("The import switch requires a database name to import to.");
}

The --import switch will require the name of a database. As soon as we get the database name, we try to open it:

$importHandle = DBI->connect("dbi:SQLite:dbname=$importDB");

This is a bit of a different format than we’ve seen before. DBI is an object. This is a special programming tool that contains code, called methods, that we can access on that object. One of the methods is the connect method. It connects to the database. Methods are very much like the subroutines we’ve already been using. They are almost always accessed using object->method(parameters).

If DBI can’t open the database, we exit and try to print an error message.

Add the help:

print "\t--import <database name>: import data into a named database\n";

You might go ahead and type “./show --import” and make sure that the help is displayed as expected.

  1. Installing from CPAN
  2. SQL database
  3. Creating tables