MCPcopy Index your code
hub / github.com/dataease/SQLBot / insert_pg

Function insert_pg

backend/apps/datasource/api/datasource.py:356–387  ·  view source on GitHub ↗
(df, tableName, engine)

Source from the content-addressed store, hash-verified

354
355
356def insert_pg(df, tableName, engine):
357 # fix field type
358 for i in range(len(df.dtypes)):
359 if str(df.dtypes[i]) == 'uint64':
360 df[str(df.columns[i])] = df[str(df.columns[i])].astype('string')
361
362 conn = engine.raw_connection()
363 cursor = conn.cursor()
364 try:
365 df.to_sql(
366 tableName,
367 engine,
368 if_exists='replace',
369 index=False
370 )
371 # trans csv
372 output = StringIO()
373 df.to_csv(output, sep='\t', header=False, index=False)
374 # output.seek(0)
375
376 # pg copy
377 query = sql.SQL("COPY {} FROM STDIN WITH CSV DELIMITER E'\t'").format(
378 sql.Identifier(tableName)
379 )
380 cursor.copy_expert(sql=query.as_string(cursor.connection), file=output)
381 conn.commit()
382 except Exception as e:
383 traceback.print_exc()
384 raise HTTPException(400, str(e))
385 finally:
386 cursor.close()
387 conn.close()
388
389
390t_sheet = "数据表列表"

Callers 1

innerFunction · 0.85

Calls 1

formatMethod · 0.80

Tested by

no test coverage detected