perl - Using DBIx::Class, how can I check if a table exists? -
my goal create function create mysql db table (using schema defined via dbix::class already) if hasn't been created yet. otherwise creates reference $schema.
currently understand how create table if doesn't exist following code:
my $schema = myapp::schema->connect( $dsn, $user, $password, ); $schema->deploy( { add_drop_table => 1 } );
i need add more logic not attempt add table if exists.
you can use "drop table if exists" in create table statement if want create every time. if don't want mess data can "show tables" , parse results, like
my $tables = $dbh->selectall_arrayref(qq|show tables|) or die "can't show tables " . $dbh->errstr(); if (@$tables) { foreach $table (@$tables) { if ($table->[0] eq 'sometablename') { # table sometablename exists } } }
Comments
Post a Comment