|
278 | 278 | "snippets": [
|
279 | 279 | {
|
280 | 280 | "title": "Create SQLite Database Table",
|
281 |
| - "description": "Creates a table in an SQLite database.", |
| 281 | + "description": "Creates a table in an SQLite database with a dynamic schema.", |
282 | 282 | "code": [
|
283 | 283 | "import sqlite3",
|
284 | 284 | "",
|
285 |
| - "def create_table(db_name, table_name):", |
| 285 | + "def create_table(db_name, table_name, schema):", |
286 | 286 | " conn = sqlite3.connect(db_name)",
|
287 | 287 | " cursor = conn.cursor()",
|
| 288 | + " schema_string = ', '.join([f'{col} {dtype}' for col, dtype in schema.items()])", |
288 | 289 | " cursor.execute(f'''",
|
289 | 290 | " CREATE TABLE IF NOT EXISTS {table_name} (",
|
290 |
| - " id INTEGER PRIMARY KEY,", |
291 |
| - " name TEXT,", |
292 |
| - " age INTEGER", |
| 291 | + " {schema_string}", |
293 | 292 | " )''')",
|
294 | 293 | " conn.commit()",
|
295 | 294 | " conn.close()",
|
296 | 295 | "",
|
297 | 296 | "# Usage:",
|
298 |
| - "create_table('example.db', 'users')" |
| 297 | + "db_name = 'example.db'", |
| 298 | + "table_name = 'users'", |
| 299 | + "schema = {", |
| 300 | + " 'id': 'INTEGER PRIMARY KEY',", |
| 301 | + " 'name': 'TEXT',", |
| 302 | + " 'age': 'INTEGER',", |
| 303 | + " 'email': 'TEXT'", |
| 304 | + "}", |
| 305 | + "create_table(db_name, table_name, schema)" |
299 | 306 | ],
|
300 | 307 | "tags": ["python", "sqlite", "database", "table"],
|
301 | 308 | "author": "e3nviction"
|
302 | 309 | },
|
303 | 310 | {
|
304 |
| - "title": "Insert Data into SQLite Table", |
305 |
| - "description": "Inserts data into a table in an SQLite database.", |
| 311 | + "title": "Insert Data into Sqlite Table", |
| 312 | + "description": "Inserts a row into a specified SQLite table using a dictionary of fields and values.", |
306 | 313 | "code": [
|
307 | 314 | "import sqlite3",
|
308 | 315 | "",
|
309 |
| - "def insert_data(db_name, table_name, name, age):", |
310 |
| - " conn = sqlite3.connect(db_name)", |
311 |
| - " cursor = conn.cursor()", |
312 |
| - " cursor.execute(f'''", |
313 |
| - " INSERT INTO {table_name} (name, age) VALUES (?, ?)''',", |
314 |
| - " (name, age))", |
315 |
| - " conn.commit()", |
316 |
| - " conn.close()", |
| 316 | + "def insert_into_table(db_path, table_name, data):", |
| 317 | + " with sqlite3.connect(db_path) as conn:", |
| 318 | + " columns = ', '.join(data.keys())", |
| 319 | + " placeholders = ', '.join(['?'] * len(data))", |
| 320 | + " sql = f\"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})\"", |
| 321 | + " conn.execute(sql, tuple(data.values()))", |
| 322 | + " conn.commit()", |
317 | 323 | "",
|
318 | 324 | "# Usage:",
|
319 |
| - "insert_data('example.db', 'users', 'John Doe', 25)" |
| 325 | + "db_path = 'example.db'", |
| 326 | + "table_name = 'users'", |
| 327 | + "data = {", |
| 328 | + " 'name': 'John Doe',", |
| 329 | + " 'email': 'john@example.com',", |
| 330 | + " 'age': 30", |
| 331 | + "}", |
| 332 | + "insert_into_table(db_path, table_name, data)" |
320 | 333 | ],
|
321 |
| - "tags": ["python", "sqlite", "database", "insert"], |
| 334 | + "tags": ["python", "sqlite", "database", "utility"], |
322 | 335 | "author": "e3nviction"
|
323 | 336 | }
|
324 | 337 | ]
|
|
349 | 362 | "categoryName": "Datetime Utilities",
|
350 | 363 | "snippets": [
|
351 | 364 | {
|
352 |
| - "title": "Get Current Date and Time", |
353 |
| - "description": "Retrieves the current date and time.", |
| 365 | + "title": "Get Current Date and Time String", |
| 366 | + "description": "Fetches the current date and time as a formatted string.", |
354 | 367 | "code": [
|
355 | 368 | "from datetime import datetime",
|
356 | 369 | "",
|
357 |
| - "def current_datetime():", |
| 370 | + "def get_current_datetime_string():", |
358 | 371 | " return datetime.now().strftime('%Y-%m-%d %H:%M:%S')",
|
359 | 372 | "",
|
360 | 373 | "# Usage:",
|
361 |
| - "print(current_datetime()) # Output: Current date and time" |
| 374 | + "print(get_current_datetime_string()) # Output: '2023-01-01 12:00:00'" |
362 | 375 | ],
|
363 |
| - "tags": ["python", "datetime", "current", "utility"], |
| 376 | + "tags": ["python", "datetime", "utility"], |
364 | 377 | "author": "e3nviction"
|
365 | 378 | },
|
366 | 379 | {
|
367 |
| - "title": "Calculate Date Difference", |
368 |
| - "description": "Calculates the difference between two dates.", |
| 380 | + "title": "Calculate Date Difference in Milliseconds", |
| 381 | + "description": "Calculates the difference between two dates in milliseconds.", |
369 | 382 | "code": [
|
370 | 383 | "from datetime import datetime",
|
371 | 384 | "",
|
372 |
| - "def date_difference(date1, date2):", |
373 |
| - " format = '%Y-%m-%d'", |
374 |
| - " d1 = datetime.strptime(date1, format)", |
375 |
| - " d2 = datetime.strptime(date2, format)", |
376 |
| - " return abs((d2 - d1).days)", |
| 385 | + "def date_difference_in_millis(date1, date2):", |
| 386 | + " delta = date2 - date1", |
| 387 | + " return delta.total_seconds() * 1000", |
377 | 388 | "",
|
378 | 389 | "# Usage:",
|
379 |
| - "print(date_difference('2024-01-01', '2024-01-10')) # Output: 9" |
| 390 | + "d1 = datetime(2023, 1, 1, 12, 0, 0)", |
| 391 | + "d2 = datetime(2023, 1, 1, 12, 1, 0)", |
| 392 | + "print(date_difference_in_millis(d1, d2))" |
380 | 393 | ],
|
381 |
| - "tags": ["python", "datetime", "difference", "utility"], |
| 394 | + "tags": ["python", "datetime", "utility"], |
382 | 395 | "author": "e3nviction"
|
383 | 396 | }
|
384 | 397 | ]
|
|
0 commit comments