Skip to content

Commit 7c7e1d2

Browse files
committed
Update consolidated snippets
1 parent 3de8665 commit 7c7e1d2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

public/consolidated/python.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,32 @@
614614
],
615615
"contributors": [],
616616
"code": "import sqlite3\n\ndef insert_into_table(db_path, table_name, data):\n with sqlite3.connect(db_path) as conn:\n columns = ', '.join(data.keys())\n placeholders = ', '.join(['?'] * len(data))\n sql = f\"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})\"\n conn.execute(sql, tuple(data.values()))\n conn.commit()\n\n# Usage:\ndb_path = 'example.db'\ntable_name = 'users'\ndata = {\n 'name': 'John Doe',\n 'email': '[email protected]',\n 'age': 30\n}\ninsert_into_table(db_path, table_name, data)\n"
617+
},
618+
{
619+
"title": "Query Data from Sqlite Table",
620+
"description": "Fetches data from a specified SQLite table, with options for selecting specific columns and applying a WHERE clause.",
621+
"author": "pl44t",
622+
"tags": [
623+
"python",
624+
"sqlite",
625+
"database",
626+
"utility"
627+
],
628+
"contributors": [],
629+
"code": "import sqlite3\n\ndef query_table(db_path, table_name, columns='*', where_clause=None):\n with sqlite3.connect(db_path) as conn:\n cursor = conn.cursor()\n sql = f\"SELECT {columns} FROM {table_name}\"\n if where_clause:\n sql += f\" WHERE {where_clause}\"\n cursor.execute(sql)\n return cursor.fetchall()\n\n# Usage:\ndb_path = 'example.db'\ntable_name = 'users'\ncolumns = 'id, name, email'\nwhere_clause = 'age > 25'\nresult = query_table(db_path, table_name, columns, where_clause)\nfor row in result:\n print(row)\n\n"
630+
},
631+
{
632+
"title": "Update Records in Sqlite Table",
633+
"description": "Updates records in a specified SQLite table, allowing dynamic column updates and an optional WHERE clause.",
634+
"author": "pl44t",
635+
"tags": [
636+
"python",
637+
"sqlite",
638+
"database",
639+
"utility"
640+
],
641+
"contributors": [],
642+
"code": "import sqlite3\n\ndef update_table(db_path, table_name, updates, where_clause=None):\n with sqlite3.connect(db_path) as conn:\n set_clause = ', '.join([f\"{col} = ?\" for col in updates.keys()])\n sql = f\"UPDATE {table_name} SET {set_clause}\"\n if where_clause:\n sql += f\" WHERE {where_clause}\"\n conn.execute(sql, tuple(updates.values()))\n conn.commit()\n\n# Usage:\ndb_path = 'example.db'\ntable_name = 'users'\nupdates = {'name': 'Jane Doe', 'age': 28}\nwhere_clause = \"id = 1\"\nupdate_table(db_path, table_name, updates, where_clause)\n\n"
617643
}
618644
]
619645
},

0 commit comments

Comments
 (0)