Next / Previous / Contents / Shipman's homepage

17.5. FileData.add(): Add a row to the table

This method adds one row to the table. Because some file names are in Unicode, and because SQLite handles all strings as Unicode, we attempt to decode the file name here. Some file names will not encode properly; we will ignore those.

deduper
# - - -   F i l e D a t a . a d d

    def add(self, path, hash, size):
        '''Add one row to the db.
        '''
        #-- 1
        # [ if path can be decoded to Unicode as UTF-8 ->
        #     uPath  :=  that path as Unicode
        #   else -> return ]
        try:
            uPath = path.decode('utf_8')
        except UnicodeDecodeError:
            return

Now create an instance of the mapped row class and add it to the database. Because we created the self._Session with autocommit=False, we'll need to commit the transaction explicitly.

deduper
        #-- 2
        # [ self  :=  self with a new row added using path=uPath,
        #             hash=(hash), and size=(size) ]
        pathHash = self.PathHash(uPath, hash, size)
        self.s.add(pathHash)
        self.s.commit()