import psycopg2 conn = psycopg2.connect(database="sdp",user="kiki",password="123",host="",port="5432") cursor = conn.cursor() stas_sql = "select * from table_a group by vin" cursor.execute(stats_sql) res_rows = cursor.fetchall() tp_arr = [] simple = {} for row in res_rows: for item in row: # 处理行数据 for res in eval(item): if len(simple) == 0: simple = res tp = tuple(res.values()) tp_arr.append(tp) cols = ", ".join('{}'.format(k) for k in simple.keys()) val_cols = ','.join('%s'.format(v) for v in simple.keys()) sql = "insert into temp.table_b (%s) values (%s)" insert_sql = sql % (cols,val_cols) cursor.executemany(res_sql, tp_arr) conn.commit() conn.close()
标签:Python,psycopg2,sql,cols,tp,cursor,simple,PG,res From: https://www.cnblogs.com/irobotzz/p/16723621.html