The Perl DBI module connects to the PostgreSQL database

Source: Internet
Author: User

Recently, I was working on a website. Because I took over the website last week, and the boss requested that the website be completed by next Monday. I am not familiar with PHP templates and PostgreSQL databases. I used to use MySQL more often. Although the Code of that website is Php, it has been completely unrecognizable, and the website system is Ubuntu Linux. At present, there is a lot of data to be imported into the database (From Around 1986 to the present ), phpPgAdmin cannot be remotely accessed, and even the Perl DBI module does not exist. I installed it for one afternoon and it was not successful. It is a nightmare for me.
Fortunately, I installed the Perl DBI module in my notebook. Why not log on from the local machine?
A sudden flash of light flashed in front of me, just as a person who fell into the abyss grabbed a straw. I studied the ipvs configuration file and asked her to allow remote connection. Then, I wrote a Database Connection Program locally using Perl DBI. After a variety of depressing transcoding problems, I finally completed it all this afternoon.
Now I have pasted the database connection code to help people who encounter the same problem.
For Perl DBI programming, the premise is that the DBI module and DBD: PG module have been installed on the local machine.
[Note] install the Perl Module

1. $ CPAN <br/> 2. $ install DBI <br/> 3. $ install DBD: PG <br/> test environment SuSE 10.1 <br/> kernel 2.6.16, Perl 5.8.8, remote server PostgreSQL 8.1 <br/> -------------------------------------------------- <br/> #! /Usr/bin/perl-W <br/> # copyright @ <br/> # A demo program for PostgreSQL with Perl DBI. <br/> # script for importing formatted text files in a directory to the database <br/> use strict; <br/> Use dBi; <br/> my $ driver = "PG "; <br/> ################ server ################### # <br/> # remote database server parameters <br/> my $ host = "192.168.0.88 "; <br/> my $ database = "s "; <br/> ################# localhost ################ <br /># parameters used for local testing <br/> # my $ host = "localhost "; <br/> # my $ Database = "Postgres "; <br/> #################################### <br/> # You can change the password below to your own password <br/> my $ user = "username "; <br/> my $ passwd = "password"; <br/> my $ Port = 5432; </P> <p> my $ DBH = DBI-> connect ("DBI: $ DRIVER: dbname = $ database; host = $ host; Port = $ port ", $ user, $ passwd, <br/> {raiseerror => 1, autocommit => 0}) or die $ DBI: errstr; <br/> my $ something = $ DBH-> prepare ("/<br/> insert into Journal (title, Etitle, Uthor, eauthor, abstract, eabstract, keyword, ekeyword, volume, year, month) /<br/> values (?,?,?,?,?,?,?,?,?,?,?) /<Br/> ") or die" syntax error: $! /N "; <br/> my ($ title, $ Etitle, $ author, $ eauthor, $ abstract, $ eabstract, $ keyword, $ ekeyword, $ volume, $ year, $ month); <br/> # Open current directory <br/> opendir (Dir ,". ") or die (" cannot open Current Directory: $! /N "); <br/> my @ DOTS = readdir (DIR); <br/> foreach (@ dots) {<br/> chomp ($ _); <br/> If (-F $ _) and (substr ($ _,-4) eq ". TXT ") {<br/> Print" start to Load file $ _ to Database ". "/N"; <br/> my $ Len = length ($ _); <br/> $ volume = substr ($ _, 0, $ len-4 ); # volume <br/> $ year = substr ($ volume, 0,4); # year <br/> $ month = substr ($ volume,-2 ); # month <br/> If (index ($ month, 0, 0) eq "0") {<br/> $ month = substr ($ month,-1 ); <br/>} <Br/> # Print $ volume. "/t ". $ year. "/t ". $ month. "/N"; <br/> open (txtfile, $ _) or die "cannot open file $ _: $! /N "; <br/> my @ file = <txtfile>; <br/> my $ line = @ file; <br/> my $ I; <br/> for ($ I = 0; $ I <$ line; $ I = $ I + 10) <br/>{# whether need to consider the encoding problem? <Br/> $ Title = $ file [$ I]; <br/> $ Etitle = $ file [$ I + 1]; <br/> $ author = $ file [$ I + 2]; <br/> $ eauthor = $ file [$ I + 3]; <br/> # $ depart = $ file [$ I + 4]; # The Department of author <br/> $ keyword = $ file [$ I + 5]; <br/> $ ekeyword = $ file [$ I + 6]; <br/> $ abstract = $ file [$ I + 7]; <br/> $ eabstract = $ file [$ I + 8]; <br/> If ($ title ne "") & ($ author ne "") & (length ($ author) <= 350) {<br/> $ something-> execute ($ title, $ Etitle, $ author, $ eauthor, $ AB Stract, $ eabstract, $ keyword, $ ekeyword, $ volume, $ year, $ month) or die "cannot execute: $! /N "; <br/> $ DBH-> commit; <br/>}< br/>}# end for <br/> Print" #### loaded file: $ _ to database. ###/n "; <br/>}# end if <br/>}< br/> closedir (DIR ); <br/> # Disconnect the database <br/> $ DBH-> disconnect;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.