我将每个表作为一个单独的文件获取TXT文件,如何将输出组合到一个TXT文件中?
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[i]}]: {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()