Write a function that reads delimited strings from a file with names and phone numbers and uses a database to store the names as the keys and the phone numbers as the values. Take as input the filename and the name of the person who’s phone number you are looking for. How would you look up the phone number to find the name it belongs to?

Note: This question does not specify the type of delimited strings to use, but the most common assumption would be a comma-delimited string. By relational database it could mean a relational database with hash tables, like in Program 165, or a MySQL database. The former would lead to an answer like findPhoneNumber, and the latter would lead to an answer like findPhoneNumber2.

To answer the question, you would look up the phone number to find the name it belongs to by checking each of the keys to see if its value matches the phone number, then returning that name.


```
import shelve
from csv import *

def findPhoneNumber(filename, personName):
#Copy info to the database
database = shelve.open(getMediaPath("phonenumbers.db"), "c")

file = open(getMediaPath(filename), "rb")
csvfile = reader(file)
index = 0
for row in csvfile:
thisRow = {'Name' : row[0], 'PhoneNumber': row[1]}
database[str(index)] = thisRow
index +=1

database.close()

#Read info from the database to return the phone number
phoneNumberToReturn = "Phone number not in database"
database = shelve.open(getMediaPath("phonenumbers.db"), "r")
for key in database.keys():
row = database[key]
if row['Name'] == personName:
phoneNumberToReturn = row['PhoneNumber']

return phoneNumberToReturn
---
from com.ziclix.python.sql import zxJDBC
from csv import *
def findPhoneNumber2(filename, personName):
#Copy info to the database
db =zxJDBC.connect("jdbc:mysql://localhost/test", "root", None ,
"com.mysql.jdbc.Driver")
con = db.cursor()
con.execute("create table Numbers (name VARCHAR(50), phonenumber
VARCHAR(50))")
file = open(getMediaPath(filename), "rb")
csvfile = reader(file)
for row in csvfile:
con.execute("insert into Numbers values ("+row[0]+","+row[1]+")")

#Read info from the database to return the phone number
phoneNumberToReturn = "Phone number not in database"

con.execute("select name,phonenumber from Numbers")
for i in range(0, con.rowcount):
results= con.fetchone()
if results[0] == personName:
phoneNumberToReturn = results[1]

return phoneNumberToReturn
```

Computer Science & Information Technology

You might also like to view...

Which of the following keeps track of the configuration of your network devices and let you know if something changes?

A. DHCPv6 B. CMDBs C. OOB gateways D. IPAMs

Computer Science & Information Technology

A(n) ________ is a hierarchical structure used to group and summarize related data

Fill in the blank(s) with correct word

Computer Science & Information Technology

It's expected that, by 2016, there will be almost _____ global network connections.

a. twelve billion b. nineteen billion c. three trillion d. none of the above

Computer Science & Information Technology

According to the U.S. Department Justice Guide to Disability Rights Laws, an accessible system is one that can be operated in a variety of ways and does not rely on a single sense or ability of the user.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology