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...

Analyze the following code:

public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } } a. The program has a compile error because of the semicolon (;) on the for loop line. b. The program compiles despite the semicolon (;) on the for loop line, and displays 4. c. The program compiles despite the semicolon (;) on the for loop line, and displays 14. d. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);

Computer Science & Information Technology

In most cases, you will import the data stored in a single Excel worksheet, but you can also import named ranges of data. _________________________

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

Computer Science & Information Technology