[Python] How to merge sqlite3 output into one file

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I get txt files for each table as a separate file, how can I combine the output into one txt file?

    with sqlite3.connect(r'db\test.sqlite') as conn:
    cursor = conn.cursor()
    tables = cursor.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
    for table in tables:
    rows = cursor.execute(f"SELECT * FROM {table[0]} WHERE USER_NAME COLLATE NOCASE LIKE ?",('%'+name+'%',)).fetchall()
    headers = [description[0] for description in cursor.description]

    if not rows:
    await update.message.reply_text("find no")

    else:
    row_text = f"test"

    for row in rows:
    row_text += "\n".join([f"\n\n[{table[0]}]"] + [f"[{headers}]: {cell}" for i, cell in enumerate(row)])

    with open(f"{name}.txt", "w", encoding="utf-8") as f:
    f.write(row_text)

    with open(f"{name}.txt", "rb") as f:
    await update.message.reply_document(document=f)

    conn.commit()
    conn.close()

    Continue reading...

Compartilhe esta Página