SQLAlchemy + Postgres-多个值更新

时间:2019-11-28 16:26:35

标签: python sql postgresql sqlalchemy

我不太想发表类似于this answer的声明,但是使用Python Sqlalchemy。这是我要编写的代码:

def _update(
            self,
            table: sqlalchemy.Table,
            df: pd.DataFrame,
            autocommit: bool = False):
            """
            df = pd.DataFrame(columns = ['id', 'values'])
            """

        # with new_values(id, values) as (
        #     values
        #     ('id1', '{0.1,0.0}'::real[]),
        #     ('id2', '{0.0,0.1}'::real[])
        # )
        # update schema.table as t set
        #     values = new_values.values
        #     from new_values
        # where t.id = new_values.id

        stmt = above_comments_to_sqlalchemy(df)

        response = self.session.execute(stmt)
        if autocommit:
            self.session.commit()
        return response

1 个答案:

答案 0 :(得分:-1)

以下文档:https://docs.sqlalchemy.org/en/13/dialects/postgresql.html

1)使用create_engine()

创建一个引擎

2)与connect()

打开连接

3)使用execute()

执行查询

例如:

engine = create_engine();
connection = engine.connect();

queryString = 'UPDATE <TableName> SET...'

connection.execute(queryString);