Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: perl mysql - INSERT INTO, 157 columns

by graff (Chancellor)
on May 02, 2014 at 01:52 UTC ( #1084719=note: print w/replies, xml ) Need Help??

in reply to perl mysql - INSERT INTO, 157 columns

If you created (or altered) the table in such a way that the columns in the table are ordered the same as the columns in the input data file, then you can build your insert sql statement like this:
my $sth = $db->prepare( "describe my_big_table" ); $sth->execute; my $rows = $sth->fetchall_arrayref; $sth->finish; my @column_names = map { $$_[0] } @$rows; my $insert_sql = join( '', 'insert into my_big_table (', join( ',', @column_names ), ') values (', join( ',', ( '?') x scalar @column_names ), ')' ); # check your work, if you like: print join( "\n", "=== columns:", @column_names, '=== sql:', $insert_s +ql, '' );
(updated to anonymize the table name)

Replies are listed 'Best First'.
Re^2: perl mysql - INSERT INTO, 157 columns
by Tux (Abbot) on May 02, 2014 at 12:35 UTC

    describe is not present in all databases :(

    More portable would be:

    my $sth = $dbh->prepare ("select * from foo"); $sth->execute; my @fld = @{$sth->{NAME}}; $sth->finish; $sth = do { local $" = ","; $dbh->prepare ("insert into foo (@fld) values (@{[('?') x scalar @ +fld]})"); };

    Enjoy, Have FUN! H.Merijn
      Given that the OP specifically mentioned mysql, I would not have posted the approach using "describe" if it didn't work in mysql.

      As for your alternative (for cases where "describe" doesn't work), I would hope that any type of database covered by DBD would at least support "select * from foo limit 1", and this would be a prudent usage for the task at hand, whether or not you actually need to execute the query in order to get field names.

      (BTW, thanks for the reminder about $")

        any type of database covered by DBD would at least support "select * from foo limit 1"
        Unfortunately, MS SQL Server throws an error:
        Incorrect syntax near '1'

        What works, though, is

        SELECT TOP 1 * FROM table
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        One would hope. However, last time I looked (v 11R2), Oracle still didn't support either limit or offset (you can fake it with rownum, but that's ugly).
      my $sth = $dbh->prepare ("select * from foo");

      Surely that should be

      my $sth = $dbh->prepare ("select * from foo where 1=0 ");

      Or something like that (limit, top, or whatever to not have the db think deeper than necessary...).

        Usually not: the fetch starts only at the first fetch, so no table content is actually fetched at all.

        Some DBD's do not even need the execute. They have the table/field info readily available after the prepare.

        Enjoy, Have FUN! H.Merijn

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1084719]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-06-22 02:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.