简体   繁体   中英

Sqlalchemy + Postgres - multiple values update

I neet to make a statement similar to this answer , but in Python Sqlalchemy. This is the code I'm trying to write:

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

Following the docs: https://docs.sqlalchemy.org/en/13/dialects/postgresql.html

1) Create an engine with create_engine()

2) Open a connection with connect()

3) execute a query using execute()

For example:

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

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

connection.execute(queryString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM