river-sim/dat_2_pg.py
2024-11-28 19:00:47 +08:00

32 lines
874 B
Python

import psycopg
from datetime import datetime, timedelta
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",
# "host": "111.231.71.81",
"user": "lzflood",
"password": "lzfloodpassword",
"dbname": "lzflooddb",
}
postgres_conn = psycopg.connect(**postgres_config)
postgres_cursor = postgres_conn.cursor()
for index, row in df.iterrows():
current_date = datetime(2010, 7, 20) + timedelta(hours=index)
for i in range(len(row)):
postgres_cursor.execute(
"INSERT INTO river_runoff (river_id, created_at, value) VALUES (%s, %s, %s)",
(i + 1, current_date, row[i] / 86400),
)
print("index, i", index, i)
postgres_conn.commit()
postgres_cursor.close()
postgres_conn.close()