HelloMySQL.pl - Sample Perl Program to Use MySQL

This section provides a sample Perl program, HelloMySQL.pl, which connects the MySQL server on the local machine, creates a table and inserts a record into the table.

Now we are ready to write a Perl program using the DBI module to access MySQL server. Here my first example, HelloMySQL.pl:

#- HelloMySQL.pl
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
   use DBI;
   $dbh = DBI->connect("DBI:mysql:test","root","TopSecret");
   $dbh->do("create table hello (message varchar(80))");
   $dbh->do("insert into hello (message) values ('Hello world!')");
   $sth = $dbh->prepare("select * from hello");
   while ((@row) = $sth->fetchrow_array()) {
      print "$row[0]\n";
   $dbh->do("drop table hello");

Let's start MySQL server, and run HelloMySQL.pl:

C:\herong>perl -version
This is perl 5, version 18, subversion 1 (v5.18.1) built 
for MSWin32-x86-multi-thread-64int
(with 1 registered patch, see perl -V for more detail)

C:\herong>perl HelloMySQL.pl
Hello world!

Congratulations, the sample Perl program is working. This proves that all required software and modules were installed correctly.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

Perl Programs and MySQL Servers

 Accessing MySQL Servers from Perl Programs

HelloMySQL.pl - Sample Perl Program to Use MySQL

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials


 PDF Printing Version