def select_and_update(): """" show how we can select a specific record, and then search and read through several records """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('data/personjob.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') logger.info('Find and display by selecting a spcific Person name...') aperson = Person.get(Person.person_name == 'Susan') logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + \ f' and likes to be known as {aperson.nickname}') logger.info('Search and display all Person with missing nicknames') logger.info( 'Our person class inherits select(). Specify search with .where()') logger.info('Peter gets a nickname but noone else') for person in Person.select().where(Person.nickname is None): logger.info( f'{person.person_name} does not have a nickname; see: {person.nickname}' ) if person.person_name == 'Peter': logger.info('Changing nickname for Peter') logger.info('Update the database') person.nickname = 'Painter' person.save() else: logger.info(f'Not giving a nickname to {person.person_name}') logger.info( 'And here is where we prove it by finding Peter and displaying') aperson = Person.get(Person.person_name == 'Peter') logger.info( f'{aperson.person_name} now has a nickname of {aperson.nickname}') except Exception as e: logger.info(e) finally: database.close()
def show_integrity_del(): """ demonstrate how database protects data inegrity : delete """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('../data/personjob.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') logger.info('Try to Delete a person who has jobs...') with database.transaction(): aperson = Person.get(Person.person_name == 'Andrew') logger.info( f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}' ) aperson.delete_instance() except Exception as e: logger.info('Delete failed because Andrew has Jobs') logger.info(f'Delete failed: {aperson.person_name}') logger.info(e) finally: database.close()
def cant_change_pk(): """ show that PKs cant be changed (and that there is no error!) """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("Back to Person class: try to change Peter's name") aperson = Person.get(Person.person_name == 'Peter') logger.info(f'Current value is {aperson.person_name}') logger.info('Update Peter to Peta, thereby trying to change the PK...') database = SqliteDatabase('../data/personjob.db') try: try: with database.transaction(): aperson = Person.get(Person.person_name == 'Peter') aperson.person_name = 'Peta' aperson.save() logger.info(f'Tried to change Peter to {aperson.person_name}') except Exception as e: logger.info(f'Cant change a PK and caught you trying' ) # not caught; no error thrown by Peewee logger.info(e) aperson = Person.get(Person.person_name == 'Peter') logger.info(f'Looked for Peter: found! -> {aperson.person_name}') try: aperson = Person.get(Person.person_name == 'Peta') except Exception as e: logger.info( f'Looking for Peta results in zero records. PK changes are ignored and do not throw an ' f'error!!!!') logger.info(f'Cant change a PK') logger.info( 'PK "change" can only be achieved with a delete and new create' ) finally: database.close()
def add_and_delete(): """" show how we can add a record, and delete a record """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('../data/personjob.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') logger.info('Add and display a Person called Fred; then delete him...') logger.info('Add Fred in one step') new_person = Person.create(person_name='Fred', lives_in_town='Seattle', nickname='Fearless') new_person.save() logger.info('Show Fred') aperson = Person.get(Person.person_name == 'Fred') logger.info( f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}' ) logger.info('but now we will delete him...') # The record we last got is the one that's kind of like in focus # So in this case Fred is in focus. He is in the aperson variable. # so if we say aperson.delete_instance we will be deleting Fred aperson.delete_instance() logger.info( 'Reading and print all Person records (but not Fred; he has been deleted)...' ) # We prove that Fred is gone by showing the logger.info() f string and seeing that # Fred is not in the database. for person in Person: logger.info( f"{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}" ) except Exception as e: logger.info(e) finally: database.close()
def add_and_delete(): """" show how we can add a record, and delete a record """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('../data/personjob.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') logger.info('Add and display a Person called Fred; then delete him...') logger.info('Add Fred in one step') new_person = Person.create(person_name='Fred', lives_in_town='Seattle', nickname='Fearless') new_person.save() logger.info('Show Fred') aperson = Person.get(Person.person_name == 'Fred') logger.info( f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}' ) logger.info('but now we will delete him...') aperson.delete_instance() logger.info( 'Reading and print all Person records (but not Fred; he has been deleted)...' ) for person in Person: logger.info( f"{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}" ) except Exception as e: logger.info(e) finally: database.close()
def setUp(self): database = SqliteDatabase('data/personjob.db') database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') self.test_person = Person.get(Person.person_name == "Andrew")