Tuesday, 15 July 2014

Sorting Perl with Class::DBI -


You have the following table named Pets:

  Name Age Pets ---- - ------------------- Carroll 25 Faucet Stan 23 Cat Mail 24 Dog Rich 24 Rabbit Mydbserver in a MySQL database on the server  
With the password of 'password' with the user of 'user'

Do the following:

1) Create a class :: dbi connection with the above credentials, this database (DBI.

2) Create a class for table pet (stomach PM)

3) Create a program that prints all names in the pets table and how many people If he / she has been sorted by name, he is of age.

Here is the code I wrote .....

  #! / Usr / bin / perl package belt :: dbi; Use DBI; Strict use; Use Base 'Class :: DBI'; Stomach :: dbi- & gt; Set_db ('main', 'dbi: myskell: dna', 'user', 'password') or $ dbi :: IRSRIS "\ n"; 1; Package Pet :: Pets; Use Base 'Pet :: DBI'; Strict use; use caution; Pets :: & gt; Table ('pet'); Stomach :: pets- & gt; Column (all = & gt; qw / name age pet /); 1; Stomach :: use pets; My Pets = Pet :: Pets- & gt; Retrieve; (Sort {$ a-> name cmp $ b-> name}} {$ a-> Age  $ b-> Age = @}) {print "name" : ". $ _- & gt; Name '= & gt; '. "Age". $ _- & gt; Age "\ n"; } 1;  

This is basically correct, but there are many minor problems.

This DBI is not required to load, Class :: DBI will take care of it for you.

You should use set_db ("main", ...) instead. Comes with set_db IMA :: DBI and it is not polite (or necessary) to bend under such a hood.

While it is not documented directly in class :: DBI (this should be), its legacy, there is no need to check the DBI errors. And if the connection fails then it will throw an error.

You have a typo, warning to use; Use warnings instead of ; .

Unless you have left three files for the post, if the code is in the same file then 1; The statements do not do anything Usage Stomach :: Pets will not work because no pet / pet PM is not a file. You do not have to use that category which is already in the same file.

In general, avoid using $ _ if you do not have a lot of things to quietly use or change it, instead of loop to my code < Code> such as a proper variable.

Sort takes only one block, but you're basically correct. This should be sorted ({a-> name cmp $ b-> name}) ($ A-> age age)} @ PETS

To avoid reading completely, a potentially large, table of contents should be sorted, actually a ORDER BY name ASC, age ASC Is done in the database and then one row is reused using one. Unfortunately, does not support any options You can use the retrieve_from_sql to add the arbitrary SQL at the end of the original selection. My $ all_people = Pet :: Pets-> Retrieve_from_sql ("Order ASC, by Age ASC"); Then your data will be sorted and one line can be read at a time. while (my $ person = $ all_people-> next) {...}

You are missing . in "age". $ _- & gt; Age "\ n" .

The tap value in the database comes back as undef, you would like to check that if $ _-> Pet is defined and do not use some other string like "no pet" or just a blank ".

You are printing the person's age, the question asks for their pets.

Otherwise, this should work.

But in fact, tell us who gave this homework to prevent people from using the class :: DBI. If they want, they can email me, schwern@pobox.com.


No comments:

Post a Comment