The code remains the same its the function that changes

Your program expects the find details() function to return a hash representing the surfer's details. Rather than the function searching the file for the data, it needs to search the TVN database, convert what the database provides to a hash, and then return the hash to the calling code.

All you need to know is which database system TVN is using and how to access it from your function.

Let's base your code on TVN's code.

I asked our technical people and they told me we use SQLite3... whatever that is. They also gave me some code for you to amend., they told me you'd know what to do with it, which is just as well because it's all gobbledygook to me!

Reaps Bake

Reaps Bake

Import the standard import sqlite3

SQLite3 library p q and connect to the data in the database file (which you can download from this book's website).

db sqlite3.connect("surfersDB.sdb")

Process each of the rows...

ID of 104.

db.row factory = sqlite3.Row cursor = db.cursor()

cursor.execute("select * from surfers rows = cursor.fetchall() for row in rows:

print("ID is " + str(row['id'])) print("Name is " + row['name']) print("Board-type is " + row['board' cursor.close()

Grab all the surfer data from the database, assigning the data to a variable called rows .

Print out some of the data (if we have a match).

Tidy up after yourself (always a good idea).

Tidy up after yourself (always a good idea).

ItTpos^ improve the efficiency a;d power f th'V.C°dT,;y0SQkL0WHaaweverr, ' littlebit about SQL. We are deliberately avo^ng -«p^^ TVNs SQL- However< w^stronglybredomQend "Head First SQL* to those wW want to learn more.

Rewrite your function to retrieve the data it needs from the TVN database. Use the ready bake code as provided by TVN's technical people as a basis for your code. Bear in mind that your program expects the data in the hash returned from the find details () function to be a collection of strings.

grab the data grab the data

You were to rewrite your function to retrieve the data it needs from the TVN database. You were to use the ready bake code as provided by TVN's technical people. You were to bear in mind that your program expects the data in the hash returned from the find details() function to be a collection of strings.

import sqlite3

Grab all the surfer data from the database, as opposed—> S to the f^. • •• • • • Cursor. = . .db.Cursorv).

. Curs.or.e.x.eCute(ws.elett. .*. from. .surfers")..

rows = Cursor.fetchall()

for row in rows: When a match is found-. if rowCid'J == id2find:

key-value pair at a time. . . . . . .sCc.ou.ntry'.] . = . r.owCl.Co.un.tr.y']..

.sCa.ve.rage']. . =. .str(r.owCaverage'.]). sCboard']. . . .=.rowCboard']..

Return the hash cursorc|ose()

return(s)

cursor.close()

to the Calling Code (as before).'

Was this article helpful?

0 0

Post a comment