bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

My boss asked me recently why we are using Perl DBI module to access our MySQL database. He asked if there was a specific reason why we have to go through an 'interface' to access our database instead of simply using the MySQL client.

I gave him the following answers but not sure if there are more:

  • Code is cleaner to look at.
  • DBI prevents SQL injection.
  • If we need to switch to another database, we simply need to change the module that we are currently using.
  • Replies are listed 'Best First'.
    Re: Advantage of using DBI
    by ikegami (Patriarch) on Jul 02, 2009 at 21:17 UTC

      He asked if there was a specific reason why we have to go through an 'interface' to access our database instead of simply using the MySQL client.

      If you mean the command line tool, it's also an interface to the library DBD::mysql uses. The difference is that the command line tool is an interface designed for interaction with humans, while DBD::mysql is an interface designed for interaction with code.

      Given that the command line tool is meant for human use, you'll need to write a whole library's worth of code to properly make requests and parse responses.

      • That's a lot of work that's already been done for you. And very well done at that. Redoing it will take a lot of work, particularly in getting the kinks ironed out.

      • It'll take a long time because it's inherently fragile to use a human interface as an API. You'll be facing intermittent, subtle and hard to debug problems down the line.

      • In particular, do you think MySQL pays any special interest is keeping the output of the command line tool unchanged? Patching or upgrading could break your code.

      • And that means you'll have to maintain and fix this library, while DBD::mysql is well tested and there's a lot of outside interest in keeping it working.

      • And when MySQL is no longer optimal for your company, your codebase will be useles.

      Does your boss think he'll be saving time? He's dead wrong.

    Re: Advantage of using DBI
    by Joost (Canon) on Jul 02, 2009 at 19:51 UTC
      MySQL comes with 2 basic interfaces: the libmysqlclient library, which provides C level access to mysql, and the mysql program, which provides a "human interface" to mysql (and which uses libmysqlclient itself).

      When you're programming against an external program/system like a database, you generally want a precise and well-documented API, which means you use the the provided API and not try to hack the human interface if at all possible.

      The DBI / DBD::mysql library is and has been THE standardized and supported interface for using libmysqlclient from perl for more than a decade. There is no other perl/mysql interface that's as well supported and fully functional. The old Msql/Mysql modules are ages behind the current functionality and Net::MySQL seems to be still in beta.

    Re: Advantage of using DBI
    by mzedeler (Pilgrim) on Jul 02, 2009 at 20:18 UTC

      SQL is an interface too. If your boss doesn't like interfaces, you should start writing code that manipulates the database files directly.

    Re: Advantage of using DBI
    by Anonymous Monk on Jul 02, 2009 at 19:43 UTC
      I'd like to ask your boss why not use a standard interface that's been fully backwards compatible for many years?

      The overhead introduced by DBI is insignificant for most practical purposes. It is simple, intuitive and provides all the mechanisms you need to write safe, efficient and (as you mention) easily portable code.

    Re: Advantage of using DBI
    by bichonfrise74 (Vicar) on Jul 02, 2009 at 22:17 UTC
      Thank you for all your responses. Yes, he meant the MySQL CLI, command line interface. He has this idea that it is simpler to code using this method instead of using the DBI.

      This stems from the fact as Ikegami mentioned that the CLI was meant for human interaction and it is easier for my boss to test his SQL on the CLI instead of using the DBI.

      But again, thanks for your inputs.

        Using the CLI to send commands from perl has two obvious problems: it is slow and its very hard (if not impossible) to use blobs. There is a host of other problems to handle that all stem from the fact that CLIs are meant run in batch or batch mode. Neither one of them is well suited for access from a script.

        Update: "meant to run in batch mode" should read "meant to run in interactive mode".

    Re: Advantage of using DBI
    by Anonymous Monk on Jul 02, 2009 at 18:57 UTC
      I didn't get what you mean with "MySQL client", I mean, the only MySQL client I can think of is the command line program to access the mysql database which is kind impossible complicated to compare with perl+DBI..

      also AFAIK, DBI executes whatever you tell him to, there's no "prevents SQL injection"
        also AFAIK, DBI executes whatever you tell him to, there's no "prevents SQL injection"

        That's right, but only half the truth. DBI does offer a very simple interface to bind parameters, making it very easy to write code that never interpolates values into SQL strings, thus preventing SQL injections.

    Re: Advantage of using DBI
    by Burak (Chaplain) on Jul 02, 2009 at 20:42 UTC
      Resign and look for a new job :p