Ask DBMS Expert


Home >> DBMS

Database Assignment

Overview: We will create a basic database program.

Much of this assignment is conceptually similar to the earthquake example; look at that code (in the in-class examples) for guidance.

The earthquake example required a more complicated input, two splits were necessary. Only one split is needed for this assignment.

Description: Databases contain records. Each record contains information about an item. Each piece of information in a record is called a field.

You may choose the type of information you would like your database to store.

There must be at least three pieces of information stored for each record, you may store more.

Some possible databases are:

Phonebook:

Last name

First name

Phone number

Library:

Book title

Author

Subject/Category

Music:

Song title

Performer

Album

Other:

...

...

...

The description will be in terms of the Phonebook, make appropriate adjustments if you do something different.

Phonebook sample_data.

The program is comprised of 3 files (see below also):

1. database.py - The provided file. You may adjust this according to your database details if you want.

2. database_mod.py - The file that contains the functions you write. Put your functions in the file database_mod.py.

3. database.txt - The file that contains the phonebook data. Put your functions in the file database_mod.py.

The provided database program file:

database.py

When run the program will provide the user with a menu from which to choose an operation, carry out the selected operation, and repeat. The operations are:

• Look up a record by the last name
• Look up a record by the first name
• Look up a record by the phone number
• Output all records formatted nicely
• Add a record.

Also:

• When run the program will automatically load the data from a file.

• When done the program will automatically save the data to a file.

Development: Details for each function is provided in the next section.

In software development it is a good idea to get a simple version working that has rudimentary functionality, and then add functionality incrementally until the program performs as required.

Develop your program using the following steps.

1. Copy the contents of the provided database start file. You may adjust it as appropriate to your database later.

2. Get the provided file to execute without error.

Do this by adding a stub for each function in the

database_mod.py file.

A stub is a function definition that does simply outputs a message and/or returns a value.

For example:

def open_db(file_name):

print('open_db called', file_name)

With the print for each stub also print the value of each parameter.

As you add stubs for other functions and run your program you can see the values sent from the call to the definition on each call. The first value for the remaining calls will be None bacause the open_db function returns None by default.

Once you have stubs for each function that is called in the provided main function (except show_menu, the definition for it is provided) you can run the program without error.

Run your program, if you have not done so already. The program should run at this point but of course it will not bahave correctly because the functions do not do what they need to yet.

If there is an error message about a missing function you missed one, write a stub for the missing function and run the program again.

3. Implement a preliminary version of the open_db that simply inputs lines and ouputs the line that was input.

Run your program. You should see lines from your input file outputted.

4. Remove the print's from open_db.

Store the the lines that were input in the list, and return the list.

Look at the File I/O in the earthquake in-class examples.

Run your program. Since there is no way to get output yet it is hard to tell if the program is inputting correctly.

5. Implement the output_all method.

Initially the function should simply output each item in the db list.

Run your program, observe the outputted list.

You now know that the program is inputting from the file and that the list has the lines of the file in it.

6. Have the open db split each line on whatever splitter character you are using (the sample data above used a colon to separate the fields).

Use the strip on each of the values from the split.

Put the result of the split in the list that is returned so that the returned list is a list of lists.

Run your program, observe the outputted list is now a list of lists.

Get this to work.

You now have a good start.

7. Get the output_all function to work correctly

8. Implement the remaining functions.

The Functions: The program will contain the following (or similar) functions:

1. open_db(file_name)

Summary: This function opens the database and loads the data from a file.

Parameters:

• file_name - The name of the file that will be opened.

Return: Return the result list.

Implementation Notes:

This function, when complete, should:

• Open the file whose name is given in the parameter variable

• Create an empty result list

• Input each line from the file (use a loop)

• Use split on the line

• Use the strip method on each field. For example:

record[0] = record[0].strip()

This will remove leading and trailing whitespace so the

• comparisons in the search functions will work correctly. Append the list you get from the split to the result list

• When done inputting the file (after the loop) close the file and return the result list.

2. output_all(db)

Summary: This function outputs all data of the database in a nicely formatted manner

Parameters:

• db - The "database", the list of lists.

Return: No return is needed.

Implementation Notes:

• Use a loop to go through the db list and call the

output_record function.

3. output_record(db, idx)

Summary: This function outputs a single database record. This function is used so that output is consistent. It is also called in the provided code.

Parameters:

• db - The "database", the list of lists.

• idx - The index of the record to be output.

Return: No return is needed.

Implementation Notes:

• Output the item from the database list db at the given index.

• Use string formatting (the % formatting operator).

4. lookup_last_name(db, name)

Summary: Search the database for the given name.

Parameters:

• db - The "database", the list of lists.

• name - The name to search for.

Return: The index where the item was found. Return None if not found.

Implementation Notes:

• Search the database list, db, for the name in the parameter name. (Use a loop.)

• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.

5. lookup_first_name(db, name)

Summary: Search the database for the given name.

Parameters:

• db - The "database", the list of lists.

• name - The name to search for.

Return: The index where the item was found. Return None if not found.

Implementation Notes:

• Search the database list, db, for the name in the parameter name. (Use a loop.)

• This function is similar to the first search function, the difference is it is comparing the parameter value to a different item in the sub-list.

• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.

6. lookup_number(db, number)

Summary: Search the database for the given number.

Parameters:

• db - The "database", the list of lists.

• number - The number to search for.

Return: The index where the item was found. Return None if not found.

Implementation Notes:

• Search the database list, db, for the number in the parameter number. (Use a loop.)

• This function is similar to the first search function, the difference is it is comparing the parameter value to a different item in the sub-list.

• Note that the number is a string so it may have dashes in it if desired.

• The search must be case insensitive. If the user enters a string that differs only in case then a match should be found.

7. add_record(db, first_name, last_name, number)

Summary: Add a record to the database

Parameters:

• db - The "database", the list of lists.
• first_name - The first name for the record.
• last_name - The last name for the record.
• number - The number for the record.

Return: No return is needed.

Implementation Notes:

• Create a list of the three paramter field values and append it to the db list.

8. close_db(db, file_name)

Summary: This function saves the contents to a file, the same file that the open_db opens and reads from when the program starts.

Parameters:

• db - The "database", the list of lists.

• file_name - The name of the file that will be opened.

Return: No return is needed.

Implementation Notes:

• Open the file specified by the parameter file_name.
• Use a loop to output each database item to the file.
• Output the field separator also.
• When done outputing to the file (after the loop) close the file.

Make a backup copy of your input file.

When a file is opened for writing/saving its previous contents are erased. If your program fails after this the orginal contents will be lost. If you have a backup of the data (phonebook) file you can copy it in and save some typing in recreating the original data file.

DBMS, Programming

  • Category:- DBMS
  • Reference No.:- M91778723

Have any Question?


Related Questions in DBMS

Data mining assignment -in this assignment you are asked to

Data Mining Assignment - In this assignment you are asked to explore the use of neural networks for classification and numeric prediction. You are also asked to carry out a data mining investigation on a real-world data ...

Sql query assignment -for this assignment you are to write

SQL Query Assignment - For this assignment you are to write your answers in a word document. This assignment is in three parts: Part A (reporting queries), Part B (query performance), Part C (query design). For this assi ...

The groceries datasetimagine 10000 receipts sitting on your

The groceries Dataset Imagine 10000 receipts sitting on your table. Each receipt represents a transaction with items that were purchased. The receipt is a representation of stuff that went into a customer's basket. That ...

You are in a real estate business renting apartments to

You are in a real estate business renting apartments to customers. Your job is to define an appropriate schema using SQL DDL in MySQL. The relations are Property(Id, Address, NumberOfUnits), Unit(ApartmentNumber, Propert ...

Objectivethe objective of this lab is to be familiar with a

OBJECTIVE: The objective of this lab is to be familiar with a process in big data modeling. You're required to produce three big data models using the MS PowerPoint software. This tool is available on UMUC Virtual Deskto ...

The relation memberstudentid organizationid roleid stores

The relation Member(StudentId, OrganizationId, RoleId) stores the membership information of student joining organization. For example, ('S1', 'O2', 'R3') indicates that student with Id 'S1' joined the organization with i ...

Relational database exerciseyou have been assigned to a new

Relational Database Exercise: You have been assigned to a new development team. A client is requesting a relational database system to manage their present store with the anticipation of adding more stores in the future. ...

Relational database design a given the following business

Relational Database Design A) Given the following business rules, identify entity types, attributes (at least two attributes for each entity, including the primary key) and relationships, and then draw an Entity-Relation ...

We can represent a data set as a collection of object nodes

We can represent a data set as a collection of object nodes and a collection of attribute nodes, where there is a link between each object and each attribute, and where the weight of that link is the value of the object ...

Data model development and implementationpurpose of the

Data model development and implementation Purpose of the assessment (with ULO Mapping) The purpose of this assignment is to develop data models and map Database System into a standard development environment to gain unde ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As