|
a |
|
b/Data_loading_in_MySQL.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 14, |
|
|
6 |
"id": "e71d86f0-1736-48e2-a77e-f1484c224eef", |
|
|
7 |
"metadata": {}, |
|
|
8 |
"outputs": [ |
|
|
9 |
{ |
|
|
10 |
"name": "stdout", |
|
|
11 |
"output_type": "stream", |
|
|
12 |
"text": [ |
|
|
13 |
"All existing data deleted from medications table.\n", |
|
|
14 |
"1000 rows inserted.\n", |
|
|
15 |
"2000 rows inserted.\n", |
|
|
16 |
"3000 rows inserted.\n", |
|
|
17 |
"4000 rows inserted.\n", |
|
|
18 |
"5000 rows inserted.\n", |
|
|
19 |
"6000 rows inserted.\n", |
|
|
20 |
"7000 rows inserted.\n", |
|
|
21 |
"8000 rows inserted.\n", |
|
|
22 |
"9000 rows inserted.\n", |
|
|
23 |
"10000 rows inserted.\n", |
|
|
24 |
"All data inserted. Total rows: 10000\n", |
|
|
25 |
"Data reload completed successfully.\n", |
|
|
26 |
"MySQL connection is closed\n" |
|
|
27 |
] |
|
|
28 |
} |
|
|
29 |
], |
|
|
30 |
"source": [ |
|
|
31 |
"import mysql.connector\n", |
|
|
32 |
"import csv\n", |
|
|
33 |
"import sys\n", |
|
|
34 |
"from datetime import datetime\n", |
|
|
35 |
"\n", |
|
|
36 |
"# Database connection details\n", |
|
|
37 |
"db_config = {\n", |
|
|
38 |
" 'host': 'localhost',\n", |
|
|
39 |
" 'user': 'root',\n", |
|
|
40 |
" 'password': 'HunnyS@1511',\n", |
|
|
41 |
" 'database': 'patient_readmission'\n", |
|
|
42 |
"}\n", |
|
|
43 |
"\n", |
|
|
44 |
"# File path\n", |
|
|
45 |
"file_path = r\"C:\\Users\\Hp\\Desktop\\IITK\\patient readmission project\\medications2.csv\"\n", |
|
|
46 |
"\n", |
|
|
47 |
"def convert_date(date_string):\n", |
|
|
48 |
" try:\n", |
|
|
49 |
" return datetime.strptime(date_string, '%d-%m-%Y').strftime('%Y-%m-%d')\n", |
|
|
50 |
" except ValueError:\n", |
|
|
51 |
" return None\n", |
|
|
52 |
"\n", |
|
|
53 |
"def delete_and_load_data(cursor, conn):\n", |
|
|
54 |
" try:\n", |
|
|
55 |
" # Delete all data from medications table\n", |
|
|
56 |
" cursor.execute(\"DELETE FROM medications\")\n", |
|
|
57 |
" print(\"All existing data deleted from medications table.\")\n", |
|
|
58 |
"\n", |
|
|
59 |
" # Read and insert data from CSV\n", |
|
|
60 |
" with open(file_path, 'r') as file:\n", |
|
|
61 |
" csv_reader = csv.reader(file)\n", |
|
|
62 |
" headers = next(csv_reader) # Get column names\n", |
|
|
63 |
" \n", |
|
|
64 |
" # Prepare the INSERT query\n", |
|
|
65 |
" columns = ', '.join(headers)\n", |
|
|
66 |
" placeholders = ', '.join(['%s'] * len(headers))\n", |
|
|
67 |
" query = f\"INSERT INTO medications ({columns}) VALUES ({placeholders})\"\n", |
|
|
68 |
" \n", |
|
|
69 |
" # Insert data row by row\n", |
|
|
70 |
" for i, row in enumerate(csv_reader, start=1):\n", |
|
|
71 |
" try:\n", |
|
|
72 |
" # Convert dates\n", |
|
|
73 |
" row[3] = convert_date(row[3]) # start_date\n", |
|
|
74 |
" row[4] = convert_date(row[4]) # end_date\n", |
|
|
75 |
" \n", |
|
|
76 |
" cursor.execute(query, row)\n", |
|
|
77 |
" if i % 1000 == 0: # Commit every 1000 rows\n", |
|
|
78 |
" conn.commit()\n", |
|
|
79 |
" print(f\"{i} rows inserted.\")\n", |
|
|
80 |
" except mysql.connector.Error as err:\n", |
|
|
81 |
" print(f\"Error at row {i}: {err}\")\n", |
|
|
82 |
" print(f\"Problematic row: {row}\")\n", |
|
|
83 |
" conn.rollback()\n", |
|
|
84 |
" \n", |
|
|
85 |
" # Final commit\n", |
|
|
86 |
" conn.commit()\n", |
|
|
87 |
" print(f\"All data inserted. Total rows: {i}\")\n", |
|
|
88 |
"\n", |
|
|
89 |
" except mysql.connector.Error as err:\n", |
|
|
90 |
" print(f\"Error: {err}\")\n", |
|
|
91 |
" conn.rollback()\n", |
|
|
92 |
"\n", |
|
|
93 |
"def main():\n", |
|
|
94 |
" try:\n", |
|
|
95 |
" conn = mysql.connector.connect(**db_config)\n", |
|
|
96 |
" cursor = conn.cursor()\n", |
|
|
97 |
"\n", |
|
|
98 |
" delete_and_load_data(cursor, conn)\n", |
|
|
99 |
"\n", |
|
|
100 |
" print(\"Data reload completed successfully.\")\n", |
|
|
101 |
"\n", |
|
|
102 |
" except mysql.connector.Error as err:\n", |
|
|
103 |
" print(f\"Database connection error: {err}\")\n", |
|
|
104 |
" finally:\n", |
|
|
105 |
" if 'conn' in locals() and conn.is_connected():\n", |
|
|
106 |
" cursor.close()\n", |
|
|
107 |
" conn.close()\n", |
|
|
108 |
" print(\"MySQL connection is closed\")\n", |
|
|
109 |
"\n", |
|
|
110 |
"if __name__ == \"__main__\":\n", |
|
|
111 |
" main()" |
|
|
112 |
] |
|
|
113 |
}, |
|
|
114 |
{ |
|
|
115 |
"cell_type": "code", |
|
|
116 |
"execution_count": null, |
|
|
117 |
"id": "99c6729e-7692-4d82-8d7d-250cdd75baf9", |
|
|
118 |
"metadata": {}, |
|
|
119 |
"outputs": [], |
|
|
120 |
"source": [] |
|
|
121 |
} |
|
|
122 |
], |
|
|
123 |
"metadata": { |
|
|
124 |
"kernelspec": { |
|
|
125 |
"display_name": "Python 3 (ipykernel)", |
|
|
126 |
"language": "python", |
|
|
127 |
"name": "python3" |
|
|
128 |
}, |
|
|
129 |
"language_info": { |
|
|
130 |
"codemirror_mode": { |
|
|
131 |
"name": "ipython", |
|
|
132 |
"version": 3 |
|
|
133 |
}, |
|
|
134 |
"file_extension": ".py", |
|
|
135 |
"mimetype": "text/x-python", |
|
|
136 |
"name": "python", |
|
|
137 |
"nbconvert_exporter": "python", |
|
|
138 |
"pygments_lexer": "ipython3", |
|
|
139 |
"version": "3.9.18" |
|
|
140 |
} |
|
|
141 |
}, |
|
|
142 |
"nbformat": 4, |
|
|
143 |
"nbformat_minor": 5 |
|
|
144 |
} |