MySQL database to excel spreadsheet report

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sport Girl
    New Member
    • Jul 2007
    • 42

    MySQL database to excel spreadsheet report

    Hi everybody ,

    i have the task of developing in Perl a script that retrieves data from 3 tables ( bugs, profiles, products) from a MySQL database called bugs and display them in an excel sheet report.

    I always get when trying to test it " 500 Internal Server Error - The server encountered an internal error or misconfiguratio n and was unable to complete your request."

    Can somebody help me please.



    //connect to MySQL database

    #!/usr/bin/perl –w
    //# -*- Mode: perl; indent-tabs-mode: nil -*-

    //use lib qw(.);

    //use Bugzilla;
    //use Bugzilla::Bug; # EmitDependList

    //use Bugzilla::Const ants; # LOGIN_*
    //use Bugzilla::User; # UserInGroup

    //require "globals.pl ";



    use DBI;
    use strict;
    use warnings;

    use Spreadsheet::Si mpleExcel;



    //my $cgi = Bugzilla->cgi;
    //my $dbh = Bugzilla->dbh;
    //my $template = Bugzilla->template;
    //my $vars = {};



    #data
    my @data1 = (['Row1Col1', 'Row1Col2'],
    [Row2Col1', 'Row2Col2']);

    my $worksheet = ['Report.xls', {-data => \@data1}];

    print “Available Database Drivers:\n\n”;
    print join(“\n”, DBI - > availableDriver s()), “\n\n”;

    //my %attr => (RaiseError => 0);

    my $dbh = DBI -> connect(“DBI:my sql:dbname=bugs :localhost”, “carole”, “carole”, { RaiseError => 1, PrintError => 0,

    AutoCommit => 0 })
    or die(“Error: $DBI::errstr”);

    my $excel_file = ‘C:/Perl/Report.xls’;

    //-e checks to see if the file exists

    if (-e “$excel_file”)
    {

    #open Excel file
    my $Book = $Excel -> Workbooks -> Open(“$excel_fi le”);
    }else{

    //paste code here to create a new file
    #create a new instance
    my $excel = Spreadsheet::Si mpleExcel -> new(-worksheets => [$worksheets]);


    //$excel ->{Visible}=1;

    #to create a file
    my $filename = 'Report.xls';
    my $excel = Spreadsheet::Si mpleExcel -> new (-filename =>$filename);


    //$excel -> { SheetsInNewWork Book} =1;

    //$workbook = $excel -> Workbooks ->Add();

    //$worksheet = $ Workbook -> Worksheets(1);
    //$worksheet -> {Name} = “Report”;

    #add worksheets
    $excel -> add_worksheet(' Report.xls');

    my $sth = $dbh -> prepare(‘select b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status, b.creation_ts, b.delta_ts,

    b.short_desc, b.op_sys, b.priority, b.rep_plarform, b.version, b.component_id, b.resolution, b.target_milest one,

    b.status_whiteb oard, b.votes, b.keywords, b.last_diffed, b.everconfirmed , b.reporter_acce ssible, b.cclist_access ible,

    b.estimated_tim e, b.remaining_tim e, b.deadline, b.alias, pf.login_name, pd.name
    from bugs b, profiles pf, products pd
    where bugs.assigned_t o = profiles.user_i d
    and bugs.product_id = products.id
    order by bug_id;’);

    $sth ->execute() or die $dbh ->errstr;

    while ( my $ref = $sth->fetchrow_hashr ef()) {

    print $ref ->{ 'bug_id', 'assigned_to', 'bug_file_loc', 'bug_severity', 'bug_status', 'creation_ts', 'delta_ts',

    'short_desc', 'op_sys', 'priority', 'product_id', 'rep_plarform', 'version', 'component_id', 'resolution',

    'target_milesto ne', 'status_whitebo ard', 'votes', 'keywords', 'last_diffed', 'everconfirmed' , 'reporter_acces sible',

    'cclist_accessi ble', 'estimated_time ', 'remaining_time ', 'deadline', 'alias', 'login_name', 'name'}, "\n";

    }

    $sth -> finish();

    #create the spreadsheet
    $excel -> output();

    #sort data of worksheet – ASC or DESC
    $excel -> sort_data(‘Repo rt.xls’,0, ‘ASC’);

    #add headers to 'Report'
    $excel -> set_headers('Re port.xls', [qw/Bugs report/]);



    #print sheet-names
    print join( “, “,$excel -> sheets()), “\n”;

    #get the result as a string
    my $spreadsheet = $excel -> output_as_strin g();

    #print result into a file and handle error
    $excel->output_to_file (“Report.xls”) or die $excel -> errstr();
    $workbook -> SaveAs($excel_f ile);

    $dbh -> disconnect();
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by Sport Girl
    Hi everybody ,

    i have the task of developing in Perl a script that retrieves data from 3 tables ( bugs, profiles, products) from a MySQL database called bugs and display them in an excel sheet report.

    I always get when trying to test it " 500 Internal Server Error - The server encountered an internal error or misconfiguratio n and was unable to complete your request."

    Can somebody help me please.


    [code=perl]
    //connect to MySQL database

    #!/usr/bin/perl –w
    //# -*- Mode: perl; indent-tabs-mode: nil -*-

    //use lib qw(.);

    //use Bugzilla;
    //use Bugzilla::Bug; # EmitDependList

    //use Bugzilla::Const ants; # LOGIN_*
    //use Bugzilla::User; # UserInGroup

    //require "globals.pl ";



    use DBI;
    use strict;
    use warnings;

    use Spreadsheet::Si mpleExcel;



    //my $cgi = Bugzilla->cgi;
    //my $dbh = Bugzilla->dbh;
    //my $template = Bugzilla->template;
    //my $vars = {};



    #data
    my @data1 = (['Row1Col1', 'Row1Col2'],
    [Row2Col1', 'Row2Col2']);

    my $worksheet = ['Report.xls', {-data => \@data1}];

    print “Available Database Drivers:\n\n”;
    print join(“\n”, DBI - > availableDriver s()), “\n\n”;

    //my %attr => (RaiseError => 0);

    my $dbh = DBI -> connect(“DBI:my sql:dbname=bugs :localhost”, “carole”, “carole”, { RaiseError => 1, PrintError => 0,

    AutoCommit => 0 })
    or die(“Error: $DBI::errstr”);

    my $excel_file = ‘C:/Perl/Report.xls’;

    //-e checks to see if the file exists

    if (-e “$excel_file”)
    {

    #open Excel file
    my $Book = $Excel -> Workbooks -> Open(“$excel_fi le”);
    }else{

    //paste code here to create a new file
    #create a new instance
    my $excel = Spreadsheet::Si mpleExcel -> new(-worksheets => [$worksheets]);


    //$excel ->{Visible}=1;

    #to create a file
    my $filename = 'Report.xls';
    my $excel = Spreadsheet::Si mpleExcel -> new (-filename =>$filename);


    //$excel -> { SheetsInNewWork Book} =1;

    //$workbook = $excel -> Workbooks ->Add();

    //$worksheet = $ Workbook -> Worksheets(1);
    //$worksheet -> {Name} = “Report”;

    #add worksheets
    $excel -> add_worksheet(' Report.xls');

    my $sth = $dbh -> prepare(‘select b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status, b.creation_ts, b.delta_ts,

    b.short_desc, b.op_sys, b.priority, b.rep_plarform, b.version, b.component_id, b.resolution, b.target_milest one,

    b.status_whiteb oard, b.votes, b.keywords, b.last_diffed, b.everconfirmed , b.reporter_acce ssible, b.cclist_access ible,

    b.estimated_tim e, b.remaining_tim e, b.deadline, b.alias, pf.login_name, pd.name
    from bugs b, profiles pf, products pd
    where bugs.assigned_t o = profiles.user_i d
    and bugs.product_id = products.id
    order by bug_id;’);

    $sth ->execute() or die $dbh ->errstr;

    while ( my $ref = $sth->fetchrow_hashr ef()) {

    print $ref ->{ 'bug_id', 'assigned_to', 'bug_file_loc', 'bug_severity', 'bug_status', 'creation_ts', 'delta_ts',

    'short_desc', 'op_sys', 'priority', 'product_id', 'rep_plarform', 'version', 'component_id', 'resolution',

    'target_milesto ne', 'status_whitebo ard', 'votes', 'keywords', 'last_diffed', 'everconfirmed' , 'reporter_acces sible',

    'cclist_accessi ble', 'estimated_time ', 'remaining_time ', 'deadline', 'alias', 'login_name', 'name'}, "\n";

    }

    $sth -> finish();

    #create the spreadsheet
    $excel -> output();

    #sort data of worksheet – ASC or DESC
    $excel -> sort_data(‘Repo rt.xls’,0, ‘ASC’);

    #add headers to 'Report'
    $excel -> set_headers('Re port.xls', [qw/Bugs report/]);



    #print sheet-names
    print join( “, “,$excel -> sheets()), “\n”;

    #get the result as a string
    my $spreadsheet = $excel -> output_as_strin g();

    #print result into a file and handle error
    $excel->output_to_file (“Report.xls”) or die $excel -> errstr();
    $workbook -> SaveAs($excel_f ile);

    $dbh -> disconnect();
    [/code]
    First, please be sure to put all of your code inside of the code tags. This way our wonderful moderator(s) don't have to follow up behind you and clean up your posting. You can find a sample of code tags in the Reply Guidelines next to your message window when submitting a message.

    (Miller: Can you please do this for the original post. ;-|) )

    As for your issue, it sounds like you are not able to get to the database ( I am assuming that is where the error is coming from). Have you tried connecting to the database by hand, with the same information in the code? This will ensure that:

    1. The database is running.
    2. The code you are using will work when the time comes.

    Try this and let us know what happens.

    Regards,

    Jeff

    Comment

    Working...