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)
postgresqlconn.py 文件源码
python
阅读 33
收藏 0
点赞 0
评论 0
评论列表
文章目录