Page 207 -
P. 207

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




                                       def find_details(id2find):
                                           db = sqlite3.connect(“surfersDB.sdb")
                 Grab all the surfer
                 data from the             db.row_factory = sqlite3.Row
                 database, as opposed
                 to the file.              cursor = db.cursor()
                                           cursor.execute(“select * from surfers")

                                           rows = cursor.fetchall()

                                           for row in rows:
                   When a match is found...          if row[‘id'] == id2find:


                                                   s = {}
                                                   s[‘id']      = str(row[‘id'])

                     ...build the hash one               s[‘name']    = row[‘name']
                     key-value pair at a
                     time.                         s[‘country'] = row[‘country']
                                                   s[‘average'] = str(row[‘average'])

                                                   s[‘board']   = row[‘board']

                                                   s[‘age']     = str(row[‘age'])


                         Return the hash              cursor.close()
                         to the calling               return(s)
                         code (as before).
                                           cursor.close()

                                           return({})





           172    Chapter 5
   202   203   204   205   206   207   208   209   210   211   212