def store_beer(beer): """ stores (or overwrites) a beer """ fermenting_from = str(misc_utils.get_storeable_date_timestamp(beer.fermenting_from_timestamp)) fermenting_to = str(misc_utils.get_storeable_date_timestamp(beer.fermenting_to_timestamp)) dryhopping_from = str(misc_utils.get_storeable_date_timestamp(beer.dryhopping_from_timestamp)) dryhopping_to = str(misc_utils.get_storeable_date_timestamp(beer.dryhopping_to_timestamp)) conditioning_from = str(misc_utils.get_storeable_date_timestamp(beer.conditioning_from_timestamp)) conditioning_to = str(misc_utils.get_storeable_date_timestamp(beer.conditioning_to_timestamp)) # does the beer already exist? cursor.execute("SELECT * FROM " + BEERS_TABLE_NAME + " WHERE beer_id='" + str(beer.beer_id) + "'") results = cursor.fetchall() if len(results) != 0: # update update_sql = "UPDATE " + BEERS_TABLE_NAME + " SET name='" + beer.name + "',style='" + beer.style + "',fermenting_from='" + fermenting_from + "',fermenting_to='" + fermenting_to + "',dryhopping_from='" + dryhopping_from + "',dryhopping_to='" + dryhopping_to + "',conditioning_from='" + conditioning_from + "',conditioning_to='" + conditioning_to + "',rating='" + str(beer.rating) + "',comments='" + beer.comments + "' WHERE beer_id='" + str(beer.beer_id) + "'" cursor.execute(update_sql) print 'Updated beer #' + str(beer.beer_id) + ' named "' + beer.name + '"' else: # create if _is_memory_db(): insert_sql = "INSERT INTO " + BEERS_TABLE_NAME + " (beer_id,name,style,fermenting_from,fermenting_to,dryhopping_from,dryhopping_to,conditioning_from,conditioning_to,rating,comments) VALUES (" + str(beer.beer_id) + ",'" + beer.name + "','" + beer.style + "','" + fermenting_from + "','" + fermenting_to + "','" + dryhopping_from + "','" + dryhopping_to + "','" + conditioning_from + "','" + conditioning_to + "'," + str(beer.rating) + ",'" + beer.comments + "')" else: insert_sql = "INSERT INTO " + BEERS_TABLE_NAME + " (name,style,fermenting_from,fermenting_to,dryhopping_from,dryhopping_to,conditioning_from,conditioning_to,rating,comments) VALUES ('" + beer.name + "','" + beer.style + "','" + fermenting_from + "','" + fermenting_to + "','" + dryhopping_from + "','" + dryhopping_to + "','" + conditioning_from + "','" + conditioning_to + "'," + str(beer.rating) + ",'" + beer.comments + "')" cursor.execute(insert_sql) print 'Created beer "' + beer.name + '"' db.commit()
def __str__(self): dryhopping_text = ""; if (self.dryhopping_from_timestamp != -1) & (self.dryhopping_to_timestamp != -1): dryhopping_text = '\nDry hopping from ' + str(misc_utils.get_storeable_date_timestamp(self.dryhopping_from_timestamp)) + ' to ' + str(misc_utils.get_storeable_date_timestamp(self.dryhopping_to_timestamp)) return 'Beer "' + self.name + '", style: ' + self.style + ', rating: ' + str(self.rating) + '/10. Comments: ' + self.comments + '\nFermenting period: ' + str(misc_utils.get_storeable_date_timestamp(self.fermenting_from_timestamp)) + ' to ' + str(misc_utils.get_storeable_date_timestamp(self.fermenting_to_timestamp)) + '\nConditioning period: ' + str(misc_utils.get_storeable_date_timestamp(self.conditioning_from_timestamp)) + ' to ' + str(misc_utils.get_storeable_date_timestamp(self.conditioning_to_timestamp)) + dryhopping_text