MySQL RENAME TABLE error

Discussion in 'Perl Misc' started by John, Oct 17, 2009.

  1. John

    John Guest

    Hi

    This is just a snip of a program I don't know if it's a Perl or MySQL
    problem
    Basically, there is a list of tables in the array 'all'.
    Prioblem = the program will create backups of all except the first table.
    I can re-arrange the tables in the array 'all', but the first one still does
    not get a backup created.

    Any ideas?
    John




    for (my $j=0; $j<$total; $j++) {
    my $table=$all[$j] ;
    create_MySQL ($table)
    }

    sub create_MySQL {
    my ($table)=@_;
    my $backup='old_' . $table;

    # backup existing table
    my ($dbh,$sql,$sth);
    $dbh=$library->MySQL_connection;
    print "<hr>$table<hr>";
    $sql="DROP TABLE IF EXISTS $backup"; $sth=$dbh->prepare($sql);
    $sth->execute or die "Unable to execute query: $dbh->errstr<br>";
    $sql="RENAME TABLE $table TO $backup"; $sth=$dbh->prepare($sql);
    $sth->execute or die "Unable to execute query: $dbh->errstr<br>";
     
    John, Oct 17, 2009
    #1
    1. Advertising

  2. John

    smallpond Guest

    On Oct 17, 8:09 am, "John" <> wrote:
    > Hi
    >
    > This is just a snip of a program  I don't know if it's a Perl or MySQL
    > problem
    > Basically, there is a list of tables in the array 'all'.
    > Prioblem = the program will create backups of all except the first table.
    > I can re-arrange the tables in the array 'all', but the first one still does
    > not get a backup created.
    >
    > Any ideas?
    > John
    >


    Somewhere above, you need to have:

    use warnings;
    use strict;

    Otherwise everyone on this group will be mad at you.

    > for (my $j=0; $j<$total; $j++) {
    >     my $table=$all[$j] ;
    >     create_MySQL ($table)
    >
    > }


    Perl arrays know how big they are, so there is no need
    for a separate variable $total.

    This code is ok, but a more perlish way to write it would be:

    foreach my $table (@all) {
    create_MySQL($table);
    }


    >
    >  sub create_MySQL {
    >       my ($table)=@_;
    >      my $backup='old_' . $table;
    >
    >       # backup existing table
    >       my ($dbh,$sql,$sth);
    >       $dbh=$library->MySQL_connection;
    >      print "<hr>$table<hr>";


    Add delimiters around the name so you know there aren't extra chars:
    print "<hr>::$table::<hr>";

    >       $sql="DROP TABLE IF EXISTS $backup"; $sth=$dbh->prepare($sql);
    >       $sth->execute or die "Unable to execute query: $dbh->errstr<br>";
    >       $sql="RENAME TABLE $table TO $backup"; $sth=$dbh->prepare($sql);
    >      $sth->execute or die "Unable to execute query: $dbh->errstr<br>";


    Certainly looks like the perl is ok. Questions:
    1) Is the first table appearing in the html output?
    2) If so, how do you know the backup is not being created?
     
    smallpond, Oct 17, 2009
    #2
    1. Advertising

  3. John

    smallpond Guest

    On Oct 17, 3:59 pm, smallpond <> wrote:
    > On Oct 17, 8:09 am, "John" <> wrote:
    >
    > > Hi

    >
    > > This is just a snip of a program  I don't know if it's a Perl or MySQL
    > > problem
    > > Basically, there is a list of tables in the array 'all'.
    > > Prioblem = the program will create backups of all except the first table.
    > > I can re-arrange the tables in the array 'all', but the first one stilldoes
    > > not get a backup created.

    >
    > > Any ideas?
    > > John

    >
    > Somewhere above, you need to have:
    >
    > use warnings;
    > use strict;
    >
    > Otherwise everyone on this group will be mad at you.
    >
    > > for (my $j=0; $j<$total; $j++) {
    > >     my $table=$all[$j] ;
    > >     create_MySQL ($table)

    >
    > > }

    >
    > Perl arrays know how big they are, so there is no need
    > for a separate variable $total.
    >
    > This code is ok, but a more perlish way to write it would be:
    >
    > foreach my $table (@all) {
    >   create_MySQL($table);
    >
    > }
    >
    > >  sub create_MySQL {
    > >       my ($table)=@_;
    > >      my $backup='old_' . $table;

    >
    > >       # backup existing table
    > >       my ($dbh,$sql,$sth);
    > >       $dbh=$library->MySQL_connection;
    > >      print "<hr>$table<hr>";

    >
    > Add delimiters around the name so you know there aren't extra chars:
    >       print "<hr>::$table::<hr>";

    ^^
    not a good choice for delimiters. with more coffee i would say:
    print "<hr>__${table}__<hr>";


    >
    > >       $sql="DROP TABLE IF EXISTS $backup"; $sth=$dbh->prepare($sql);
    > >       $sth->execute or die "Unable to execute query: $dbh->errstr<br>";
    > >       $sql="RENAME TABLE $table TO $backup"; $sth=$dbh->prepare($sql);
    > >      $sth->execute or die "Unable to execute query: $dbh->errstr<br>";

    >
    > Certainly looks like the perl is ok.  Questions:
    > 1) Is the first table appearing in the html output?
    > 2) If so, how do you know the backup is not being created?
     
    smallpond, Oct 17, 2009
    #3
  4. John

    John Guest

    "smallpond" <> wrote in message
    news:...
    On Oct 17, 3:59 pm, smallpond <> wrote:
    > On Oct 17, 8:09 am, "John" <> wrote:
    >
    > > Hi

    >
    > > This is just a snip of a program I don't know if it's a Perl or MySQL
    > > problem
    > > Basically, there is a list of tables in the array 'all'.
    > > Prioblem = the program will create backups of all except the first
    > > table.
    > > I can re-arrange the tables in the array 'all', but the first one still
    > > does
    > > not get a backup created.

    >
    > > Any ideas?
    > > John

    >
    > Somewhere above, you need to have:
    >
    > use warnings;
    > use strict;
    >
    > Otherwise everyone on this group will be mad at you.
    >
    > > for (my $j=0; $j<$total; $j++) {
    > > my $table=$all[$j] ;
    > > create_MySQL ($table)

    >
    > > }

    >
    > Perl arrays know how big they are, so there is no need
    > for a separate variable $total.
    >
    > This code is ok, but a more perlish way to write it would be:
    >
    > foreach my $table (@all) {
    > create_MySQL($table);
    >
    > }
    >
    > > sub create_MySQL {
    > > my ($table)=@_;
    > > my $backup='old_' . $table;

    >
    > > # backup existing table
    > > my ($dbh,$sql,$sth);
    > > $dbh=$library->MySQL_connection;
    > > print "<hr>$table<hr>";

    >
    > Add delimiters around the name so you know there aren't extra chars:
    > print "<hr>::$table::<hr>";

    ^^
    not a good choice for delimiters. with more coffee i would say:
    print "<hr>__${table}__<hr>";


    >
    > > $sql="DROP TABLE IF EXISTS $backup"; $sth=$dbh->prepare($sql);
    > > $sth->execute or die "Unable to execute query: $dbh->errstr<br>";
    > > $sql="RENAME TABLE $table TO $backup"; $sth=$dbh->prepare($sql);
    > > $sth->execute or die "Unable to execute query: $dbh->errstr<br>";

    >
    > Certainly looks like the perl is ok. Questions:
    > 1) Is the first table appearing in the html output?
    > 2) If so, how do you know the backup is not being created?


    Hi

    OK. Many thanks. I know there is no backup as I use PhpMyAdmin - a nice
    GUI - to look at the tables.
    So I now know it's a MySQL problem. I least I know where to look.

    Regards
    John
     
    John, Oct 19, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathy Burke
    Replies:
    1
    Views:
    451
    Ron Vecchi
    Jul 26, 2003
  2. Kathy Burke
    Replies:
    0
    Views:
    426
    Kathy Burke
    Jul 27, 2003
  3. Cirene
    Replies:
    3
    Views:
    756
    Cirene
    May 13, 2008
  4. julian
    Replies:
    8
    Views:
    470
    Avatar
    Apr 6, 2006
  5. Sam
    Replies:
    4
    Views:
    77
    Mark Lawrence
    Jan 17, 2014
Loading...

Share This Page