Simple Perl Tutorial to Insert Data into MySQL

In this tutorial i will describe how to create connection string, with help this connection we will insert record in mysql database using Perl script. Below script use mysql as database to store data and Perl dbi module use to make connection with mysql.

This is very simple tutorial to learn Perl script and i hope it will very useful to Perl beginner developer.
You can also check other tutorial of perl,

Below is simple script to insert record in database.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!C:/Perl/bin/perl.exe

#testdb.pl
use warnings;
use strict;
# PERL DBI CONNECT
use DBI;
#use DBD::mysql;

print "Content-Type: text/html; charset=iso-8859-1\n\n";

# CONFIG VARIABLES
my $platform = "mysql";
my $database = "zftest";
my $host = "localhost";
my $port = "3306";
my $tablename = "users";
my $user = "root";
my $pw = "";
# DATA SOURCE NAME
my $dsn = "dbi:mysql:$database:localhost:3306";

# PERL DBI CONNECT
my $connect = DBI->connect($dsn, $user, $pw);

# PREPARE THE QUERY
my $query = "INSERT INTO $tablename (firstname, lastname, email, username, password) VALUES ('pine','apples', 'testgmail.com', 'parvez1476', 'kkkk')";
my $query_handle = $connect->prepare($query);