Gossamer Forum
Quote Reply
creating new tables
Using a Creator can I check for the existence of a table? Or do I need to try and create a Table or Editor object instead? Is there a specific function that does the check or is it a case of attempting to create a Table or Editor, if that fails assume the table doesn't exist and then get a Creator?



Thanks,

Tommy
Quote Reply
Re: [tommyfotak] creating new tables In reply to
Unfortunately, there is no function that checks if a table exists or not at the moment.

You can do something like

Code:
eval { $table = $DB->table('MyTable'); };
if ($@) {
$creator = $DB->creator ....
}
or you can check if the .def file exists.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] creating new tables In reply to
Thanks Yogi, I'll give that a bash.
Quote Reply
Re: [tommyfotak] creating new tables In reply to
Basically, if you look at the install.pm of some of GT's plugins, you attempt to do what you want to do, then read the error messages to see what went wrong.

You can always attempt to connect to a table, and if that fails, attempt to create it. If the creation fails, you know there is some sort of database problem. If the creation succeeds, you know you've successfully created it.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [tommyfotak] creating new tables In reply to
Is there a way to check for table existance in 3.x?
RGB World, Inc. - Software & Web Development.
rgbworld.com
Quote Reply
Re: [rgbworld] creating new tables In reply to
I hope Ian wont mind as I think this is his code but this is what I use to check for a table:

sub hastable {
#---------------------------
# Determine if a table exists
# Return true if it does.
my $table = shift;
my $exists = 0;

my $def_path = "$CFG->{admin_root_path}/defs/" . $DB->prefix() . "$table.def";

if (-e $def_path) { $exists = 1; }

return $exists;
}


then in your script you can use

if ( hastable('TableName')) {print 'TableName table found.<br>'; } else {create table...}