postgresqlconn.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:heroku-python-boilerplate 作者: chavli 项目源码 文件源码
def insert_one(self, table_name: str, row: dict, retcol: str):
        """ inserts a single row into the specified table and returns the value of the specified
        column.

        row - a dict containing all row values to insert

        returns: new id
        """
        if not self.__conn or not row:
            raise psycopg2.InterfaceError("null connection")

        try:
            columns = "(" + ",".join(row.keys()) + ")"
            holders = "(" + ",".join(["%s"] * len(row.keys())) + ")"
            fillers = tuple(row.values())
            query = "insert into {table_name} {columnnames} values {values} returning {retcol}".format(**{
                "table_name": table_name,
                "columnnames": columns,
                "values": holders,
                "retcol": retcol
                })

            with self.__conn.cursor() as curs:
                curs.execute(query, fillers)
                return curs.fetchone()[0]

        except Exception as e:
            tb.print_exc()
            raise(e)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号