patternsqlMinor
Perl DBI Sample with MySQL DDL
Viewed 0 times
sampleperlwithddlmysqldbi
Problem
I've been experimenting with Perl and MySql and wrote this code for connecting and writing to a database:
Is this the correct/idiomatic way to access a database in Perl? Are there any other improvements I could make?
# MySQL DDL to create database used by code
#
# CREATE DATABASE sampledb;
#
# USE sampledb;
#
# CREATE TABLE `dbtable` (
# `id` int(11) NOT NULL AUTO_INCREMENT,
# `demo` longtext,
# PRIMARY KEY (`id`)
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# PERL MODULES
use strict;
use warnings;
use DBI; #http://dbi.perl.org
# CONFIG VARIABLES
my $platform = "mysql";
my $database = "sampledb";
my $host = "localhost";
my $port = "3306";
my $username = "root";
my $password = "password";
# DATA SOURCE NAME
my $dsn = "dbi:$platform:$database:$host:$port";
# PERL DBI CONNECT
my $connect = DBI->connect($dsn, $username, $password);
# VARS for Examples
my $query;
my $query_handle;
my $id;
my $demo;
# Example 1 using prepare() and execute() INSERT
# SAMPLE VARIABLE AND VALUES TO PASS INTO SQL STATEMENT
$id = 1;
$demo = "test";
# INSERT
$query = "INSERT INTO dbtable (id, demo) VALUES ('$id', '$demo')";
$query_handle = $connect->prepare($query);
$query_handle->execute();
undef $query;
# Example 2 using do() UPDATE
# SAMPLE VARIABLE AND VALUES TO PASS INTO SQL STATEMENT
$id = 1;
$demo = "test 2";
# UPDATE
$query = "UPDATE dbtable SET demo = '$demo' WHERE id = $id";
$query_handle = $connect->do($query);
undef $query;Is this the correct/idiomatic way to access a database in Perl? Are there any other improvements I could make?
Solution
Couple of things I noticed:
When you connect to the database, you don't check that the connection succeeded. The most common way I've seen to do this in Perl is:
If the connection fails, the program will then display the connection failure message so you can see what's going on.
I see that you use two different ways of accessing the database. It would be better to pick one and use it for all your inserts/updates unless there is a compelling reason not to - it keeps future readers from trying to figure out why you did it differently. In this case I'd reccomend using
Also don't forget to disconnect:
When you connect to the database, you don't check that the connection succeeded. The most common way I've seen to do this in Perl is:
my $connect = DBI->connect($dsn, $username, $password)
or die "Connection Error: DBI::errstr\n";If the connection fails, the program will then display the connection failure message so you can see what's going on.
I see that you use two different ways of accessing the database. It would be better to pick one and use it for all your inserts/updates unless there is a compelling reason not to - it keeps future readers from trying to figure out why you did it differently. In this case I'd reccomend using
execute since it allows you to execute multiple times:$query_handle = $connect->prepare("INSERT INTO dbtable (id, demo) VALUES (?, ?)");
$query_handle->execute($id, $demo);
$id = 2;
$demo = "test2";
$query_handle->execute($id, $demo);Also don't forget to disconnect:
$connect->disconnect;Code Snippets
my $connect = DBI->connect($dsn, $username, $password)
or die "Connection Error: DBI::errstr\n";$query_handle = $connect->prepare("INSERT INTO dbtable (id, demo) VALUES (?, ?)");
$query_handle->execute($id, $demo);
$id = 2;
$demo = "test2";
$query_handle->execute($id, $demo);$connect->disconnect;Context
StackExchange Code Review Q#1465, answer score: 3
Revisions (0)
No revisions yet.