Skip to main content

Posts

Showing posts from August, 2008

Interesting... Badongo

Just found this site, wondering if it may be a useful way to cheaply back stuff up from one machine to another, or just a place to store backups. I work on four different machines, and have backups scattered all over the place, so I think I'll check it out! http://www.badongo.com/

Handy Perl Functions: Automatically create a form from a database table

Back after a much needed vacation with another perl code snippet. This one creates a simple form using information pulled from the structure of a database table. The form it creates is not meant to be the end product - rather it gets a lot of the "grunt work" done, and you can then edit the resulting form to change fields to drop-downs, remove fields that shouldn't be edited, etc. It will also try to guess at a decent label for each field, based on the table name. This script uses the same get_fields() function that outlined in my previous post. As always, I'm sure this isn't the "perfect" code, and of course it can be improved - but it does work, and may perhaps be a useful starting block for someone else... Usage - pretty simple - run the script and give it a database table name to use. It'll spit the html back out, which you can save in a text file or whatever. #!/usr/bin/perl # creates a basic form from the database table specified use DBI; # ...

Handy Perl Functions: get_fields()

This is the first of what may be a recurring theme - handy custom perl functions. These are custom pieces of code that I find myself using in a wide variety of places, both in dynamic websites and in maintenance scripts. This first one is called get_fields() and its function is quite simple - it returns an array of field names for the table name passed to it. I've used it for things like generating edit/new record form templates, or when writing scripts to convert from database format to another, etc. It uses the perl DBI module, and assumes that $dbh is the database handle that has been set up for use. #--------------------------------------------------------------- sub get_fields { # returns array of fields for the table name passed to it my $table = $_[0]; my (@fields, @tmp) = (); my $qry = "describe $table"; $qry = $dbh->prepare($qry); $qry->execute; while (@tmp = $qry->fetchrow_array) { ...