Add verbose option to import.py

This commit is contained in:
2023-05-28 23:06:02 +02:00
parent 12100e977e
commit 9d9ca3c036

View File

@ -45,12 +45,20 @@ def sqlite_regexp(pattern: str, value: Optional[str]) -> Optional[bool]:
return re.match(pattern, value) is not None
def import_csv(cur: sqlite3.Cursor, table_name: str) -> None:
def import_csv(cur: sqlite3.Cursor, table_name: str, verbose: bool = False) -> None:
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
names = next(rows)
sql = f'INSERT INTO {table_name} ({", ".join(names)}) VALUES ({", ".join(["?"] * len(names))})'
print(sql)
if verbose:
inserted = 0
for row in rows:
print(row)
cur.execute(sql, row)
inserted += 1
print(f'{inserted} inserts')
else:
cur.executemany(sql, rows)
print(f'{cur.rowcount} inserts')
@ -93,6 +101,8 @@ def main() -> None:
help='The sqlite database file')
parser.add_argument('-k', '--keep', action='store_true', default=False,
help='Whether the database file should be overwritten or kept')
parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='Log every inserted row')
args = parser.parse_args()
DIR = args.dir
@ -121,7 +131,7 @@ def main() -> None:
cnx.execute("PRAGMA foreign_keys = OFF")
cnx.execute("BEGIN")
for table in TABLES:
import_csv(cnx.cursor(), table)
import_csv(cnx.cursor(), table, args.verbose)
if not check_foreign_keys(cnx.cursor()):
raise RuntimeError('foreign key constraint failed')
cnx.execute("COMMIT")