Let assume I have some table in db created by reading csv file (by line). CSV file updates sometimes and I download it from remote resource. Now code looks like this:
import asyncio
import aiohttp
import aiopg
import zipfile
async def update_db():
zipfile.ZipFile(ZIP_FILENAME).extract(CSV_FILENAME)
async with aiopg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
with open(CSV_FILENAME) as file:
headers = next(file)
for line in file:
region, city = [col for col in line.split(';', COLUMN_QUANTITY)]
await cur.execute(f"select id from region where title=%s;", (region,))
response_list = list(cur)
if len(response_list) == 0:
await cur.execute(f"insert into region (title) values (%s) returning id", (region,))
region_id = list(cur)[0][0]
else:
region_id = response_list[0][0]
... ... ...
How can I detect new rows in csv and create new corteges in table without making extra queries to db (whithout checking each line from csv if it exists in table)?
I'm using windows, python3 and PostrgreSQL. When I download updated csv I have no access to old file.