I had previously asked a question regarding testing for a numeric or string value. That thread ended up discussing placeholders to eliminate the need for the test. So I tried using placeholder after reading through the links provided and could not find a solution.

Here's what I currently have (that works) but I would like to better understand how placeholders could simplify the code. This is used to create a backup for a mySQL database (Part of a mySQL table editor)

# 1. Download text-only data. Comma delimited and quoted strings for +?easy? upload. my $table = $q->param('dbtable'); # Can be any table in the database my @field_type = (); my $n = 0; my $sth = $dbh->prepare("DESCRIBE $table"); $sth->execute(); while (my @field_descr = $sth->fetchrow_array) { @field_type[$n] = ($field_descr[1] =~ /char/i) ? 1 : 0; # Set to +1 if string $n++; } $sth->finish(); $sth = $dbh->prepare("SELECT * FROM $table"); $sth->execute(); print "Content-Type: text/plain\n\n"; while (my @record = $sth->fetchrow_array()) { # Build a string of each record, comma delimited, and inserting qu +otes as needed my ($recStr,$dl,$n) = ("","",0); foreach my $field (@record) { $field =~ s/(\n|\r)//g; # Get rid of any return chars that MAY + have been erroneously uploaded. $recStr .= ($field_type[$n]) ? "$dl'$field'" : "$dl$field"; # +Insert the quotes. $dl = ","; $n++; } print "$recStr\n"; } $sth->finish(); # 2. Upload the data. my $dbdata = $q->param('dbdata'); my @filedata = <$dbdata>; foreach my $record (@filedata) { my $sql = "INSERT INTO $table VALUES ($record)"; my $rows = $dbh->do($sql); }
If I could eliminate the need for the quotes, it would greatly simplify the 'download' code. I understand that, if I knew the number of fields in the table, I could use placeholders to add the quotes; but I don't because it can be any table in the database.
my $sql = "INSERT INTO tablename VALUES(?,?,?,?)"; my $sth = $dbh->prepare($sql); foreach my $record (@filedata) { $sth->execute($fa,$fb,$fc,$fd); }
Any ideas, thoughts, or comments would be appreciated and will, hopefully :), expand my limited Perl knowledge

In reply to Understanding placeholders by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.