"""executemany.py — INSERT banyak baris dalam satu panggilan executemany().

Jalankan dari folder chapter:  python3 executemany.py
Port default 3306; timpa lewat variabel lingkungan DB_PORT.
"""

import os

import pymysql

port = int(os.environ.get("DB_PORT", "3306"))

produk_baru = [
    (4, "Susu Kental", 38000),
    (5, "Kopi Robusta", 48000),
    (6, "Kopi Luwak", 120000),
]

with pymysql.connect(
    host="127.0.0.1",
    user="root",
    password="",
    database="belajar_app",
    port=port,
) as koneksi:
    with koneksi.cursor() as kursor:
        kursor.executemany(
            "INSERT INTO produk (id, nama, harga) VALUES (%s, %s, %s)",
            produk_baru,
        )
        koneksi.commit()
        print("executemany memasukkan", kursor.rowcount, "baris")

    with koneksi.cursor() as kursor:
        kursor.execute("SELECT id, nama, harga FROM produk ORDER BY id")
        print("isi tabel produk sekarang:")
        for id_produk, nama, harga in kursor.fetchall():
            print(f"  {id_produk} | {nama} | {harga}")
