Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add CSV to SQL converter for user data #271

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions data/generator/user_insert_mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import csv


class CSVToSQLConverter:

def __init__(self, csv_file_path, sql_file_path):
self.csv_file_path = csv_file_path
self.sql_file_path = sql_file_path

def convert(self):
with open(self.csv_file_path, 'r') as csv_file:
reader = csv.DictReader(csv_file)

with open(self.sql_file_path, 'w') as sql_file:
values = []

# Iterate through each row in the csv file
for row in reader:
value = f"""(
'{row['Nickname']}',
'{row['Username']}',
'{row['ImagePath']}',
'{row['CreatedAt']}',
'{row['BackgroundImagePath']}',
'{row['Username'].lower()}@gmail.com',
'{row['Username'].lower()}',
'',
''
)"""
values.append(value)

# Create a bulk insert statement
statement = f"""
INSERT INTO user (nickname, username, profile_image_path, created_at, background_image_path, email, password, dm_pin, paint_pin)
VALUES {', '.join(values)};
"""

# Write the SQL insert statement to the file
sql_file.write(statement)


# Create an instance of the converter and use it
converter = CSVToSQLConverter('user.csv', 'user_mysql_insert.sql')
converter.convert()
Loading