I was trying to do something similar; creating a table with the column names and corresponding types that the user input (ie I assume that they know enough SQL to know what the column types should be). I found that it was easier (though arguably less elegant) to just store the column names and types in a scalar variable with the pairs just seperated by commas. You can then plug this variable straight into the SQL CREATE TABLE command.
#Get the input from the user, I have this in a subroutine which is cal
+led repeatedly until the user enters 'done'
print "Please enter the name of the column, when you are finished type
+ 'done':\n";
chomp(my $column_name=<>);
print "Please enter the column type:\n";
chomp (my $column_type=<>);
if (defined $fields) {
$fields = "$fields, $column_name $column_type";
} else {
$fields = "$column_name $column_type";
}
#Otherwise $fields begins with ', ' which is not what we want
my $create_table = "CREATE TABLE $table_name ($fields)";
#Put this in a variable so that it is all interpolated before being pa
+ssed to the SQL
my $sth=$dbh->do($create_table) or die "Could not prepare table $check
+table: $DBI::errstr\n";
print "Table $table_name created\n"
This method is somewhat adapted from
this article. Hope it makes sense =)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.