29 lines
721 B
Python
29 lines
721 B
Python
import mysql.connector
|
|
import json
|
|
|
|
# Connect to MySQL
|
|
mysql_conn = mysql.connector.connect(
|
|
host='localhost',
|
|
user='root',
|
|
password='Kfsh&rc9788',
|
|
database='trucks2db'
|
|
)
|
|
cursor = mysql_conn.cursor()
|
|
|
|
# Get list of tables
|
|
cursor.execute("SHOW TABLES")
|
|
tables = cursor.fetchall()
|
|
|
|
for table in tables:
|
|
table_name = table[0]
|
|
cursor.execute(f"SELECT * FROM {table_name}")
|
|
rows = cursor.fetchall()
|
|
cursor.execute(f"DESCRIBE {table_name}")
|
|
columns = [column[0] for column in cursor.fetchall()]
|
|
|
|
# Convert rows to list of dictionaries
|
|
data = [dict(zip(columns, row)) for row in rows]
|
|
|
|
# Write to JSON file
|
|
with open(f'{table_name}.json', 'w') as f:
|
|
json.dump(data, f) |