Hi Monks,

I'm experimenting with a perl script to automate a couple of database creation processes. I'm aware these can be done at the mysql command line but I would nevertheless like to see how it can be done with a perl script.

Here is the code:
#!/usr/bin/perl use DBI; # Changed after advice from jeffa my %conf = ( db_type => 'mysql', db_name => 'new_db', db_user => 'root', db_pass => '****', table => 'profile', ); my $dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}", $conf{db_u +ser}, $conf{db_pass}, {PrintError => 0, RaiseError => 0}); create_database(); create_table(); describe_table(); sub create_database { $dbh->do("DROP DATABASE $conf{db_name}") || warn("Warning (expected): Can't drop db '$conf{db_name}': $ +DBI::errstr"); $dbh->do("CREATE DATABASE $conf{db_name}") || die("Can't create database '$conf{db_name}': $DBI::errstr"); } sub create_table { $dbh->do("USE $conf{db_name}") || die("Can't use database '$conf{db_name}': $DBI::errstr"); $dbh->do("CREATE TABLE $conf{table} (" . 'name varchar(255) not null, ' . 'age int, ' . 'birthday varchar(255) not null, ' . 'school varchar(255) not null)') || die("Can't create table '$conf{table}': $DBI::errstr"); } sub describe_table { # I'm trying to get it to print out the table's # structure (the sort you see when you type # 'DESCRIBE table_name' in mysql. } $dbh->disconnect(); # Success. print "Success\n"; exit(0);
The code creates a database and a table. I'm trying to get it to print out the table's structure (the sort you see when you type 'DESCRIBE table_name' in mysql). I've no idea how to do that.

Any help would be appreciated.

Thanks in anticipation :)

In reply to perl-mysql help - print table structure by kiat

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.