Tham khảo tài liệu 'programming in python 3 - part 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 484 Chapter 12. Database Programming fields return fields 0 if fields is not None else None The get_director_id function returns the ID of the given director or None if there is no such director in the database. We use the fetchone method because there is either zero or one matching record. We know that there are no duplicate directors because the directors table s name field has a UNIQUE constraint and in any case we always check for the existence of a director before adding a new one. The fetch methods always return a sequence of fields or None if there are no more records even if as here we have asked to retrieve only a single field. def edit_dvd db title identity find_dvd db edit if title is None return title Title title title if not title return cursor SELECT FROM dvds directors WHERE AND id dict id identity year duration director director Director director director if not director return year Year year year 1896 .year duration Duration minutes minutes duration minimum 0 maximum 60 48 director_id get_and_set_director db director UPDATE dvds SET title title year year duration duration director_id director_id WHERE id identity locals To edit a DVD record we must first find the record the user wants to work on. If a record is found we begin by giving the user the opportunity to change the title. Then we retrieve the other fields so that we can provide the existing values as defaults to minimize what the user must type since they can just press Enter to accept a default. Here we have used named placeholders of the form name and must therefore provide the corresponding values using a mapping. For the SELECT statement we have used a freshly created dictionary and for the UPDATE statement we have used the .