csv出力のコードを修正その3
This commit is contained in:
parent
d640155e75
commit
e1fee8b2e5
1 changed files with 10 additions and 8 deletions
16
dislocker.py
16
dislocker.py
|
@ -2,6 +2,7 @@ import json
|
||||||
import discord
|
import discord
|
||||||
import os
|
import os
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from psycopg2 import sql
|
||||||
import hashlib
|
import hashlib
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
|
@ -198,23 +199,24 @@ class Bot(discord.Client):
|
||||||
|
|
||||||
def report_export(self, **kwargs):
|
def report_export(self, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
cursor = dislocker.db.cursor()
|
||||||
csv_file_path = self.export_dir_path + "pc_usage_history.csv"
|
csv_file_path = self.export_dir_path + "pc_usage_history.csv"
|
||||||
main_table = "pc_usage_history"
|
main_table = "pc_usage_history"
|
||||||
related_table = "club_member"
|
related_table = "club_member"
|
||||||
cursor = dislocker.db.cursor()
|
|
||||||
# メインテーブルの列情報を取得(user_idを除く)
|
# メインテーブルの列情報を取得(user_idを除く)
|
||||||
cursor.execute(psycopg2.sql.SQL("SELECT * FROM {} LIMIT 0").format(psycopg2.sql.Identifier(main_table)))
|
cursor.execute(sql.SQL("SELECT * FROM {} LIMIT 0").format(sql.Identifier(main_table)))
|
||||||
main_columns = [desc[0] for desc in cursor.description if desc[0] != 'member_id']
|
main_columns = [desc[0] for desc in cursor.description if desc[0] != 'member_id']
|
||||||
|
|
||||||
# クエリを作成(列名を明確に指定)
|
# クエリを作成(列名を明確に指定)
|
||||||
query = psycopg2.sql.SQL("""
|
query = sql.SQL("""
|
||||||
SELECT {main_columns}, {related_table}.name
|
SELECT {main_columns}, {related_table}.name
|
||||||
FROM {main_table}
|
FROM {main_table}
|
||||||
LEFT JOIN {related_table} ON {main_table}.member_id = {related_table}.id
|
LEFT JOIN {related_table} ON {main_table}.member_id = {related_table}.id
|
||||||
""").format(
|
""").format(
|
||||||
main_columns=psycopg2.sql.SQL(', ').join([psycopg2.sql.SQL("{}.{}").format(psycopg2.sql.Identifier(main_table), psycopg2.sql.Identifier(col)) for col in main_columns]),
|
main_columns=sql.SQL(', ').join([sql.SQL("{}.{}").format(sql.Identifier(main_table), sql.Identifier(col)) for col in main_columns]),
|
||||||
main_table=psycopg2.sql.Identifier(main_table),
|
main_table=sql.Identifier(main_table),
|
||||||
related_table=psycopg2.sql.Identifier(related_table)
|
related_table=sql.Identifier(related_table)
|
||||||
)
|
)
|
||||||
|
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
|
@ -230,7 +232,7 @@ class Bot(discord.Client):
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
# nameを2番目に移動
|
# nameを2番目に移動
|
||||||
formatted_row = [format_datetime(row[0])] + [row[-1]] + [format_datetime(field) if field is not None else '' for field in row[1:-1]]
|
formatted_row = [self.format_datetime(row[0])] + [row[-1]] + [self.format_datetime(field) if field is not None else '' for field in row[1:-1]]
|
||||||
csvwriter.writerow(formatted_row)
|
csvwriter.writerow(formatted_row)
|
||||||
|
|
||||||
print(f"テーブル '{main_table}' の内容を '{csv_file_path}' に出力しました。")
|
print(f"テーブル '{main_table}' の内容を '{csv_file_path}' に出力しました。")
|
||||||
|
|
Loading…
Reference in a new issue