From d0d120130b39328e5b8c3a43fbf0e5a8086316f4 Mon Sep 17 00:00:00 2001 From: baol Date: Thu, 28 Nov 2024 12:17:53 +0800 Subject: [PATCH] add feature --- dat_2_pg.py | 28 ++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 2 files changed, 32 insertions(+) create mode 100644 dat_2_pg.py create mode 100644 requirements.txt diff --git a/dat_2_pg.py b/dat_2_pg.py new file mode 100644 index 0000000..e1d534d --- /dev/null +++ b/dat_2_pg.py @@ -0,0 +1,28 @@ +import psycopg + +from read_dat import read_binary_file + +file_path = "lz.out/lz.rivqdown.dat" +df = read_binary_file(file_path) + +# PostgreSQL连接配置 +postgres_config = { + "host": "192.168.3.12", + "user": "prepare", + "password": "preparepassword", + "dbname": "preparedb", +} + +postgres_conn = psycopg.connect(**postgres_config) +postgres_cursor = postgres_conn.cursor() + +for index, row in df.iterrows(): + for i in range(len(row)): + postgres_cursor.execute( + "INSERT INTO river_flow (river_id, created_at, runoff) VALUES (%s, %s, %s)", + (i + 1, index, row[i]), + ) + +postgres_conn.commit() +postgres_cursor.close() +postgres_conn.close() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..075fe9a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +pandas +gdal +geopandas +psycopg[binary] \ No newline at end of file