-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting.py
More file actions
61 lines (49 loc) · 2.16 KB
/
Scripting.py
File metadata and controls
61 lines (49 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import mysql.connector
import json
# Database connection details
db_config = {
"host": "sql12.freesqldatabase.com", # Database host
"user": "sql12746404", # Database username
"password": "lrmtWx3KSQ", # Database password
"database": "sql12746404", # Database name
}
def insert_json_to_database(json_file_path, db_config):
"""
Function to read data from a JSON file and insert it into a MySQL database.
Parameters:
- json_file_path: Path to the JSON file containing data.
- db_config: Dictionary containing database connection details.
"""
try:
# Establish connection to the database
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
# Define the SQL query for inserting data
insert_query = "INSERT INTO employee (name, age, city) VALUES (%s, %s, %s)"
# Read the JSON file
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
# Iterate through each record in the JSON file
for user in data:
# Extract user details with default values for missing fields
name = user.get('name', 'N/A')
age = user.get('age', 0) # Default age is 0 if missing
city = user.get('city', 'N/A')
# Execute the INSERT query with the extracted data
cursor.execute(insert_query, (name, age, city))
# Commit the transaction to save changes in the database
conn.commit()
print(f"{cursor.rowcount} records inserted successfully.")
except mysql.connector.Error as err:
# Handle any database connection or execution errors
print(f"Database Error: {err}")
finally:
# Ensure the database connection is properly closed
if conn.is_connected():
cursor.close()
conn.close()
print("Database connection closed.")
# Specify the path to the JSON file
json_file_path = 'users_1K.json'
# Call the function to insert data into the database
insert_json_to_database(json_file_path, db_config)