You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UPDATE users SET age = 30 WHERE email = 'john.doe@example.com'
125
110
"""
126
111
db_ops.execute_query(update_query)
127
-
print("\u2705 User updated successfully!")
128
112
```
129
113
130
-
---
131
-
132
-
### **🔹 Delete a Record**
114
+
### 🔹 **Delete a Record**
133
115
```python
134
116
delete_query ="""
135
117
DELETE FROM users WHERE email = 'john.doe@example.com'
136
118
"""
137
119
db_ops.execute_query(delete_query)
138
-
print("\u2705 User deleted successfully!")
139
120
```
140
121
141
-
---
142
-
143
-
## **6. Saving Query Results**
122
+
### 🔹 **Create Table**
123
+
```python
124
+
create_table_sql ="""
125
+
IF NOT EXISTS (
126
+
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'users'
127
+
)
128
+
BEGIN
129
+
CREATE TABLE users (
130
+
id INT IDENTITY(1,1) PRIMARY KEY,
131
+
name VARCHAR(100),
132
+
email VARCHAR(100),
133
+
age INT,
134
+
gender VARCHAR(10),
135
+
phone_number VARCHAR(20),
136
+
address VARCHAR(255),
137
+
city VARCHAR(100),
138
+
country VARCHAR(100)
139
+
);
140
+
END
141
+
"""
142
+
db_ops.create_table(create_table_sql)
143
+
```
144
144
145
-
### **🔹 Save Data to CSV**
145
+
### 🔹 **Insert a DataFrame to SQL**
146
146
```python
147
-
db_ops.save_results(df, "users_data.csv", "csv")
148
-
print("\u2705 Data saved to users_data.csv")
147
+
import pandas as pd
148
+
149
+
# Example DataFrame
150
+
df_users = pd.DataFrame([
151
+
{
152
+
'name': 'Jane Smith',
153
+
'email': 'jane.smith@example.com',
154
+
'age': 32,
155
+
'gender': 'Female',
156
+
'phone_number': '555-555-5555',
157
+
'address': '456 Oak Ave',
158
+
'city': 'Chicago',
159
+
'country': 'USA'
160
+
}
161
+
])
162
+
163
+
db_ops.insert_dataframe(df_users, 'users')
149
164
```
150
-
📁 **Check your project folder for `users_data.csv`**
151
165
152
-
---
166
+
### 🔹 **Update SQL Table with DataFrame**
167
+
168
+
> **Note:**`key_columns` should include the column(s) used to uniquely identify each row (like `id` or `email`). These are used in the SQL `WHERE` clause to apply updates only to matching rows.
153
169
154
-
## **7. Using Caching & Batch Fetching**
155
170
156
-
### **🔹 Query with Caching**
157
171
```python
158
-
df_cached = db_ops.cached_query("SELECT * FROM users")
0 commit comments