From 3c13625e63a4dfde3128fdb2d2bba85ede0ed055 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 7 Jan 2018 01:35:27 +0000 Subject: [PATCH] Better logic for db inserts --- platypush/plugins/db/__init__.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/platypush/plugins/db/__init__.py b/platypush/plugins/db/__init__.py index f392d8c2f..72dc8eaca 100644 --- a/platypush/plugins/db/__init__.py +++ b/platypush/plugins/db/__init__.py @@ -1,6 +1,6 @@ import logging -from sqlalchemy import create_engine +from sqlalchemy import create_engine, Table, MetaData from platypush.message.response import Response @@ -33,12 +33,13 @@ class DbPlugin(Plugin): return self.engine def execute(self, statement, engine=None, *args, **kwargs): - """ Executes a generic SQL statement """ + """ Executes a raw SQL statement """ engine = self._get_engine(engine, *args, **kwargs) with engine.connect() as connection: result = connection.execute(statement) + connection.commit() return Response() @@ -65,12 +66,10 @@ class DbPlugin(Plugin): engine = self._get_engine(engine, *args, **kwargs) for record in records: - statement = 'INSERT INTO {}({}) VALUES({})'.format \ - (table, ','.join(record.keys()), - ','.join([ ':' + key for key in record.keys() ])) - - with engine.connect() as connection: - connection.execute(statement, **record) + metadata = MetaData() + table = Table(table, metadata, autoload=True, autoload_with=engine) + insert = table.insert().values(**record) + engine.execute(insert) return Response()