Muskovitz has asked for the wisdom of the Perl Monks concerning the following question:

hey perl mongers i have scripted a little program of perl/tk that will connect to mysql db and then process the query search and then prints out the results but it gets hang sometimes it wont load.

#!/usr/bin/perl use strict; use warnings; use Tk; use DBI; use DBD::mysql; our $type="mysql"; our $database="store"; our $host="somesite.com"; our $port="3306"; our $tablename="vegetables"; our $user="example"; our $pwd="*********"; our $dsn="dbi:$type:$database:$host:$port"; our $query; our $queryhandle; our $connect=DBI->connect($dsn,$user,$pwd) || die "ERROR: $!\n"; my $mw=new MainWindow; $mw->Lable(-text=>"Search for vegetables")->pack; my $veges=$mw->Entry()->pack; $mw->Button(-text=>"Search",-command=>\&search)->pack; sub search{ my $sv=$veges->get();$veges->delete(qw/0 end/); $query="SELECT * FROM vegetables WHERE vegetable LIKE '%$sv%'"; $queryhandle=$connect->prepare($query); $queryhandle->execute; while($queryhandle->fetch()){ $mw->Label(-text=>"Vegetables Found for: $sv")->pack; } } MainLoop;

I'll go for sleep for now just leave a comment guys i'll back reading it tomorrow! thanks in advance.

Replies are listed 'Best First'.
Re: Perl/Tk hang when conecting to Remote MySQL Server
by graff (Chancellor) on Dec 25, 2014 at 05:47 UTC
    When you say "sometimes it won't load", does that really mean that sometimes "it does load"? If "sometimes it works" and "sometimes it doesn't", the possible explanations include:
    • Sometimes the database connection drops out or fails after you connect. (How stable is your database server and your network connection to it?)
    • Sometimes the query result is too large, and because you aren't handling that very well, your script has a problem with memory or cpu usage (or network bandwidth).
    • The connection and the query actually do work every time, but because your Tk display logic is pretty bad right now, you can't tell that the query actually worked.
    I'm guessing that the third one is most likely what is really going on. Here's a slightly modified version of your script that might get you headed in the right direction:
    #!/usr/bin/perl use strict; use warnings; use Tk; use DBI; use DBD::mysql; our $type="mysql"; our $database="store"; our $host="somesite.com"; our $port="3306"; our $tablename="vegetables"; our $user="example"; our $pwd="*********"; our $dsn="dbi:$type:$database:$host:$port"; our $query; our $queryhandle; our $connect=DBI->connect($dsn,$user,$pwd) || die "ERROR: $!\n"; my $mw=new MainWindow; $mw->Label(-text=>"Search for vegetables")->pack; # fix misspelled "L +able" my $veges=$mw->Entry()->pack; $mw->Button(-text=>"Search",-command=>\&search)->pack; # Declare a variable and just one Label widget for displaying results: my $result_string; my $results = $mw->Label( -textvariable => \$result_string )->pack; sub search{ my $sv=$veges->get();$veges->delete(qw/0 end/); $query="SELECT count(*) FROM vegetables WHERE vegetable LIKE '%$sv +%'"; $queryhandle=$connect->prepare($query); $queryhandle->execute; my ( $hit_count ) = $queryhandle->fetchrow_array; # there's just o +ne row $result_string = sprintf( "Found %d matches for %s", $hit_count, $ +sv ); # assigning a new value to $result_string will update the Labe +l display } MainLoop;
    When you want to show an actual list of rows that are returned by a query, try using a Listbox or Text widget, something that can hold all the results in a single scrollable object.

    (As posted, your script creates a new Label widget for every row in the query result, so if the first query returns 50 rows, you get 50 Labels stacked vertically that all say the same thing. That's pretty useless - once the window expands to the bottom of the screen, you can see anything else, and those first 50 labels never go away, so you'll never see results from a second or third query.)

      Thank you for both of you this lines i include fixed out my problem

      $query="SELECT count(*) FROM vegetables WHERE vegetable LIKE '%$sv%' $mw->update(); my $results = $mw->Label( -textvariable => \$result_string )->pack; fetchrow_array

      thanks for handling out the input in just 1 scalar variable that worked out also! Problem solved!

        In your latest snippet here, I think the "update()" call isn't really doing anything. In one sense, that's not a problem, because it isn't needed at all - you can delete that line and the script will work just the same.

        If the "update()" call were necessary (which in this case it isn't), you'd want to do it after executing your query, getting the values from the database, and assigning them to variables.

Re: Perl/Tk hang when conecting to Remote MySQL Server
by chacham (Prior) on Dec 24, 2014 at 17:32 UTC

    SQL Side note: SELECT * should be used in EXISTS() and ad hoc queries. Otherwise, specify the column list. Not only does it prevent bugs due to unexpected columns and column reordering, it is self-documenting.

      yeah... but when i include this line in the main program

      use strict; use warnings; $queryhandle->bind_colums(undef, \my $id, \my $vegetable, \my $price e +tc....);

      those error lines will gone but the thing that what i ask for is its getting hang while connecting to mysql like a building a socket in perl/tk coz i am very new to perl/Tk

Re: Perl/Tk hang when conecting to Remote MySQL Server
by Gangabass (Vicar) on Dec 25, 2014 at 05:10 UTC
    Try to add $mw->update; to your while loop.