In this in-memory SQLite lab, the primary-key CSV replay kept sample and second once each in records. The records table declared key TEXT PRIMARY KEY. The without_key table retained duplicate sample and second values after repeating the INSERT OR REPLACE import.

The problem

When a CSV import is retried, check whether the stored rows still match the intended record identities. The schema gap in without_key was that key was plain TEXT with no uniqueness constraint, even though the fixture reused A and B as record keys.

Reproduce the result

The script parsed the CSV fixture with csv.reader and removed the header before importing. It opened the database with sqlite3.connect(":memory:"). The without_key table declared key and value as TEXT without a uniqueness constraint.

The script passed the same batch twice to each table with executemany and INSERT OR REPLACE. It selected key and value from records in key order after each import. It selected key and value from without_key in key order after both imports. A separate query grouped without_key by key and used COUNT(*) to count each group.

The following example ran on Python 3.11.9 with synthetic data.

import csv import io import sqlite3 text = "key,value\nA,sample\nB,second\n" batch = list(csv.reader(io.StringIO(text)))[1:] with sqlite3.connect(":memory:") as db: db.execute("CREATE TABLE records (key TEXT PRIMARY KEY, value TEXT)") for run in (1, 2): db.executemany("INSERT OR REPLACE INTO records VALUES (?, ?)", batch) rows = db.execute("SELECT key, value FROM records ORDER BY key").fetchall() assert rows == [("A", "sample"), ("B", "second")] print(run, rows) db.execute("CREATE TABLE without_key (key TEXT, value TEXT)") for run in (1, 2): db.executemany("INSERT OR REPLACE INTO without_key VALUES (?, ?)", batch) duplicates = db.execute("SELECT key, value FROM without_key ORDER BY key").fetchall() per_key = db.execute("SELECT key, COUNT(*) FROM without_key GROUP BY key ORDER BY key").fetchall() assert duplicates == [("A", "sample"), ("A", "sample"), ("B", "second"), ("B", "second")] assert per_key == [("A", 2), ("B", 2)] print("without key:", len(duplicates), duplicates) print("rows per key:", per_key) 
1 [('A', 'sample'), ('B', 'second')] 2 [('A', 'sample'), ('B', 'second')] without key: 4 [('A', 'sample'), ('A', 'sample'), ('B', 'second'), ('B', 'second')] rows per key: [('A', 2), ('B', 2)] 

What the output says

The first records readback printed “1 [('A', 'sample'), ('B', 'second')]”. The second printed “2 [('A', 'sample'), ('B', 'second')]”. The ordered key and value pairs matched across the readbacks.

The without_key readback printed “without key: 4 [('A', 'sample'), ('A', 'sample'), ('B', 'second'), ('B', 'second')]”. The grouped query printed “rows per key: [('A', 2), ('B', 2)]”.

Limits

The CSV batch pairs A with sample and B with second. The entire comparison used the same in-memory connection. The fixture did not exercise a changed value for an existing key. It did not exercise a replay after reopening a database file.

Apply the check

Test a table that declares key TEXT PRIMARY KEY with your own CSV batch, then compare the stored rows after replay. Replay the identical batch and compare the ordered key and value pairs after each import. Check an existing table for duplicate keys with COUNT(*) grouped by key.

For a changed-value test, import A with sample, replay the batch with A mapped to updated, and inspect the stored value for A. Use the in-memory fixture to compare the keyed and unkeyed results under the same test conditions.

Primary reference: Python documentation

Supporting references: SQLite PRIMARY KEY; SQLite ON CONFLICT and REPLACE

AI disclosure: This article was generated by an automated Codex editorial workflow.