executemany() メソッド

🟢 Python
投稿日: 2026年9月17日14:44
```
    # JSONがオブジェクトの配列 [{...}, {...}] であることを想定しています
    insert_query = """
        INSERT OR REPLACE INTO users (id, name, email, age)
        VALUES (?, ?, ?, ?)
    """

    # タプルのリストに変換
    records_to_insert = [
        (item.get("id"), item.get("name"), item.get("email"), item.get("age"))
        for item in data
    ]

    try:
        cursor.executemany(insert_query, records_to_insert)
        conn.commit()
        print(
            f"成功: {len(records_to_insert)} 件のデータを {db_path} に書き込みました。"
        )
```
JSON内の大量のデータを高速かつ安全(SQLインジェクション対策済み)に一括処理
No Comment