Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how to store text file information into my sql database

by chella2104@gmail.com (Sexton)
on Dec 26, 2015 at 08:42 UTC ( [id://1151176]=perlquestion: print w/replies, xml ) Need Help??

chella2104@gmail.com has asked for the wisdom of the Perl Monks concerning the following question:

call any one tell me how to store text file information into mysql database for example text file is:
StudentId Name Dept address city
1 Chellappa CSE 22 xx-colony 2nd street coimbatore
2 Vijay IT 23 yy colony coimbatore
In this file (22 xx-colony 2nd street) and (23 yy colony) is address to store in database address column

This is my code

use DBI; use strict; my $driver = "mysql"; my $database = "TESTDB"; my $dsn = "DBI:$driver:database=$database"; my $userid = "root"; my $password = "1234"; my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr; my $query = 'INSERT INTO student (StudentId,Name,Dept,address,city) VA +LUES (?,?,?,?,?)'; my $sth = $dbh->prepare($query) or die "Prepare failed: " . $dbh->errs +tr(); open my $fh, "<", "text.txt" or die $!; <$fh>; #skip header while (<$fh>) { chomp; my @vals = split; $sth->execute(@vals); } close $fh;

anyone can tell me the answer

Replies are listed 'Best First'.
Re: how to store text file information into my sql database
by poj (Abbot) on Dec 26, 2015 at 11:16 UTC

    If you have fixed width records then use unpack

    #!perl use strict; while (<DATA>){ chomp; my @vals = unpack("A10A10A5A24A10",$_); print join "|", @vals,"\n"; #$sth->execute(@vals); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore
    poj

      You can also generate the unpack string from the header itself if you know that the position of the keys in the header defines the columns in the rest of the file:

      #!/usr/bin/perl use strict; use warnings; my $header = <DATA>; my @p; push @p, length $1 while $header =~ /(\S+\s*)/g; my $unpack_pattern = join "", map { 'A'.$_ } @p[0..$#p-1]; $unpack_pattern .= "A*"; while (<DATA>){ chomp; my @vals = unpack($unpack_pattern,$_); print join "|", @vals,"\n"; #$sth->execute(@vals); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore

        There is a CPAN module DataExtract::FixedWidth for handling the fixed unpacking details. I tried it on the test sample and it seems to work fine.

        use strict; use warnings; use DataExtract::FixedWidth; use Data::Dumper; my $de = DataExtract::FixedWidth->new({ header_row => scalar(<DATA>) }); while (my $l = <DATA>) { my $fields = $de->parse($l); print Dumper($fields); } __DATA__ StudentId Name Dept address city 1 Chellappa CSE 22 xx-colony 2nd street coimbatore 2 Vijay IT 23 yy colony coimbatore
        Ron

      StudentId | Name | Dept | address | city |

      +-----------+--------+--------+-----------+------------------------+

      | 1 | a CSE | 22 x | x-colony | 2nd street coimbatore |

      | 2 | T 23 | yy col | ony coim | batore

      when i am trying this code only above values to store database for example CSE is not stored in dept column

Re: how to store text file information into my sql database
by graff (Chancellor) on Dec 27, 2015 at 00:34 UTC
    In order for you input file to work properly for this sort of thing, it must either (a) use tab characters as column delimiters (and with no tabs or new-line characters contained within any given column value) or (b) use fixed-width columns (same number of characters per column, where each column is padded with trailing spaces as needed to fill in a fixed-width character count (*)).

    If your input file is tab delimited, adjust your split statement:

    my @vals = split /\t/;
    (When you do the default "split" with no regex, it splits the string on one or more contiguous white-space characters of all kinds.)

    If the file uses fixed-width, space-padded columns, use "unpack" as described in a previous reply.

    (*) UPDATE - a better explanation of "fixed-width columns" (because my original wording was not very good): every row (line of text) would have the same number of characters; the columns in a given row can be various sizes, but each column has the same size on consecutive rows, so that column boundaries are always at the same offsets from the start of the row, and strings contained in each column would be padded with spaces as needed to maintain the character offset to the next column (or end of line).

Re: how to store text file information into my sql database
by RichardK (Parson) on Dec 26, 2015 at 11:26 UTC
Re: how to store text file information into my sql database
by Anonymous Monk on Dec 26, 2015 at 09:36 UTC
    Does it work? What happens?
      no sir that code doesn't work properly

        ... doesn't work properly

        Don't hesitate to elaborate further. What happened then? What did you expect to happen?

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

Re: how to store text file information into my sql database
by Anonymous Monk on Dec 29, 2015 at 04:16 UTC

    It looks like your data may be tab delimited, so try changing the split parameter, like this:

    while (<$fh>){ chomp; my @vals = split /\t/; $sth->execute(@vals); }

      To split on \t seems the solution at your problem (already posted above).

      my $query = 'INSERT INTO student (StudentId,Name,Dept,address,city) VA +LUES (?,?,?,?,?)';

      but the quote delimiter in this code is weak and could break easely. "insert..... "; is better.

      why don't you replace all ' by \' in the values ?

      Peace
        but the quote delimiter in this code is weak and could break easely. "insert..... "; is better.

        Why are double quotes better when there are no variables to interpolate ?

        poj
Re: how to store text file information into my sql database
by Anonymous Monk on Dec 26, 2015 at 09:36 UTC
    Does it work? What happens?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1151176]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found