yeap's profileyeap's spaceBlogGuestbookNetwork Tools Help

Blog


    July 06

    Performing SQL operations through Perl from http://www.codeproject.com/KB/perl/perldbi.aspx

    Introduction

    This article explains on how to perform operations on your database through Perl, using the DBI module. This assumes that you have basic knowledge about Perl/CGI and SQL. We will be making a simple table and performing basic SQL operations on it.

    Comments

    Like all Perl code, this code too is self explanatory. If you need detailed information, don't hesitate to use the article forums.

    Example one

    Creating a table.

    Collapse Copy Code
    #!/usr/local/bin/perl
    use DBI;
    
    $username = '';$password = '';$database = '';$hostname = '';
    $dbh = DBI->connect("dbi:mysql:database=$database;" . 
       "host=$hostname;port=3306", $username, $password);
    
    $SQL= "create table user(ID integer primary key " . 
      "auto_increment, username text not null," . 
      " password text not null, email text not null)";
    
    $CreateTable = $dbh->do($SQL);
    
    print "Content-type:text/html\n\n\n";
    if($CreateTable){
    print "Success";
    }
    else{
    print "Failure
    $DBI::errstr"; }

    Example two

    Inserting a record.

    Collapse Copy Code
    #!/usr/local/bin/perl
    use DBI;
    
    $username = '';$password = '';$database = '';$hostname = '';
    $dbh = DBI->connect("dbi:mysql:database=$database;" . 
      "host=$hostname;port=3306", $username, $password);
    
    $SQL= "insert into user (username, password, email)" .
      " values('lexxwern', 'password', 'email@host')";
    
    $InsertRecord = $dbh->do($SQL);
    
    print "Content-type:text/html\n\n\n";
    if($InsertRecord){
    print "Success";
    }
    else{
    print "Failure
    $DBI::errstr"; }

    Example three

    Updating a record.

    Collapse Copy Code
    #!/usr/local/bin/perl
    use DBI;
    
    $username = '';$password = '';$database = '';$hostname = '';
    $dbh = DBI->connect("dbi:mysql:database=$database;" .
      "host=$hostname;port=3306", $username, $password);
    
    $SQL= "update user set email = ".
      "'lexxwern@yahoo.com' where username = 'lexxwern'";
    
    $UpdateRecord = $dbh->do($SQL);
    
    print "Content-type:text/html\n\n\n";
    if($UpdateRecord){
    print "Success";
    }
    else{
    print "Failure
    $DBI::errstr"; }

    Example four

    Deleting a record.

    Collapse Copy Code
    #!/usr/local/bin/perl
    use DBI;
    
    $username = '';$password = '';$database = '';$hostname = '';
    $dbh = DBI->connect("dbi:mysql:database=$database;" .
      "host=$hostname;port=3306", $username, $password);
    
    $SQL= "delete from user where ID=1";
    
    $DeleteRecord = $dbh->do($SQL);
    
    print "Content-type:text/html\n\n\n";
    if($DeleteRecord){
    print "Success";
    }
    else{
    print "Failure
    $DBI::errstr"; }

    Example five

    Viewing all records.

    Collapse Copy Code
    #!/usr/local/bin/perl
    print "Content-type:text/html\n\n";
    
    use DBI;
    
    $username = '';$password = '';$database = '';$hostname = '';
    $dbh = DBI->connect("dbi:mysql:database=$database;" .
     "host=$hostname;port=3306", $username, $password);
    
    $SQL= "select * from user";
    
    $Select = $dbh->prepare($SQL);
    $Select->execute();
    
    while($Row=$Select->fetchrow_hashref)
    {
      print "$Row->{username}
    $Row->{email}"; }

    Conclusion

    Hopefully these examples can give you a neat preview of the capabilities of the DBI module. This site will be of further help. Good luck!

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    lexxwern


    Member
    About
    :: a fulltime college student from new delhi, india.

    Latest
    :: college 1st semester.
    Occupation: Web Developer
    Location: India India

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://cid-68aa22dffd188c8c.spaces.live.com/blog/cns!68AA22DFFD188C8C!180.trak
    Weblogs that reference this entry
    • None