diff --git a/src/database/connectors/AltBookConnector.py b/src/database/connectors/AltBookConnector.py index dcb132666f085e50f16f3e190c25f77a4c8c4778..3cbef0587617a5be30d469d60432435438a45f3a 100644 --- a/src/database/connectors/AltBookConnector.py +++ b/src/database/connectors/AltBookConnector.py @@ -2,6 +2,7 @@ from ..AltConnector import AltConnector from database.models.Book import Book from datetime import datetime + class AltBookConnector(AltConnector): def save_book(self, item): conn = self.get_connection() @@ -14,12 +15,13 @@ class AltBookConnector(AltConnector): 'image_path': item['images'][0]['path'], 'created_at': datetime.utcnow(), 'updated_at': datetime.utcnow(), - }) # TODO + }) - sql = 'INSERT INTO books({keys}) VALUES({values}) ;'.format( + sql = """INSERT INTO books({keys}) VALUES({values}) ON DUPLICATE KEY UPDATE {items};""".format( keys=', '.join(real_field.keys()), - values=', '.join(list('%s' for key in real_field.keys())) + values=', '.join('%s' for key in real_field.keys()), + items=', '.join('{k}={v}'.format(k=k, v='%s') for k in real_field.keys() if k != 'created_at') ) - cursor.execute(sql, real_field.values()) + cursor.execute(sql, list(real_field.values()) + list(v for k, v in real_field.items() if k != 'created_at')) conn.commit()