Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am used to populating MySQL tables with text files on the Linux command line. How do I do it for MS SQL Server and Perl DBI. Does anyone have any code they can share with explanations?

Replies are listed 'Best First'.
Re: Importing text files with DBI
by mpeppler (Vicar) on Nov 24, 2003 at 18:49 UTC
    Something like this could work, assuming a single SQL statememt per file:
    use strict; my $dbh = DBI->connect('dbi:...', 'user', 'pwd'); foreach my $file (@ARGV) { open(IN, $file) || die "Can't open $file: $!"; my $sql = join(' ', <IN>); close(IN); my $sth = $dbh->prepare($sql); $sth->execute() || die $sth->errstr; # Maybe you need to fetch rows here? while(my $d = $sth->fetch) { print "@$d\n"; } }
    The above is obviously very minimal (and untested) - and it you have more than one SQL statement per file you need to figure out a way to break the file into SQL statements.

    Of course you could also simply use MS-SQL's isql.exec command-line tool to run the statements...

    Michael

Re: Importing text files with DBI
by dragonchild (Archbishop) on Nov 24, 2003 at 18:48 UTC
    What kind of text files? What kind of validation? Are there any foreign keys? What about column name specifications?

    In other words, you need to be a little more specific with your question. What kind of ideas do you have? What do you think should be done?

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Importing text files with DBI
by Art_XIV (Hermit) on Nov 24, 2003 at 18:53 UTC

    You can still use the command line to import into SQL Server (if you want). Check your docs for the bcp (bulk copy) utility.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Importing text files with DBI
by RockM66 (Novice) on Nov 23, 2013 at 16:39 UTC

    Curious if there are modules to import delimnited text into, say, mysql (didn't find much on CPAN). Something like this, where $dbh is a DBI object linked to some database:

    $schema=<<EOF; `Name` VARCHAR(32) PRIMARY KEY, `Address` VARCHAR(256) NOT NULL, `Phone` VARCHAR(16) NOT NULL, `Comment` VARCHAR(80) EOF importTextIntoDBI( $dbh, 'myTable.txt', delimiter=>"\t", tableName=>'myTable', schema=>$schema, firstline=>2, # Skip header checkMissing=>[0..2], # Make sure first 3 columns are not missing, checkPattern=>[undef,undef,$phoneRegexp,under] # Check formats );

    There's Text::Delimited for example, but I didn't see support for missing values and format checking. Maybe there are other modules that do the parsing, checking, etc, on a delimited file to create a "clean" object that's ready to be imported into DBI?