|
1153 | 1153 | "source": [
|
1154 | 1154 | "[Link to SQLGlot](https://bit.ly/4dGyTmP)."
|
1155 | 1155 | ]
|
| 1156 | + }, |
| 1157 | + { |
| 1158 | + "cell_type": "code", |
| 1159 | + "execution_count": null, |
| 1160 | + "id": "e7894c33-4dab-4990-8d71-a611e1a82fc7", |
| 1161 | + "metadata": {}, |
| 1162 | + "outputs": [], |
| 1163 | + "source": [ |
| 1164 | + "!pip install sqlitedict" |
| 1165 | + ] |
| 1166 | + }, |
| 1167 | + { |
| 1168 | + "cell_type": "markdown", |
| 1169 | + "id": "46ad8730-b06f-49a1-894b-e34218f2fe31", |
| 1170 | + "metadata": {}, |
| 1171 | + "source": [ |
| 1172 | + "Writing data to SQLite directly and reading it back requires verbose SQL statements, schema definitions, and type handling, which can be tedious when storing complex Python objects or making frequent changes results in complex code." |
| 1173 | + ] |
| 1174 | + }, |
| 1175 | + { |
| 1176 | + "cell_type": "code", |
| 1177 | + "execution_count": 14, |
| 1178 | + "id": "9f61565c-724c-47ca-a675-cd4aa93a594b", |
| 1179 | + "metadata": {}, |
| 1180 | + "outputs": [], |
| 1181 | + "source": [ |
| 1182 | + "import sqlite3\n", |
| 1183 | + "\n", |
| 1184 | + "products_to_update = [\n", |
| 1185 | + " (\"P1\", \"Laptop\", 999.99, 50),\n", |
| 1186 | + " (\"P2\", \"Mouse\", 29.99, 100),\n", |
| 1187 | + " (\"P3\", \"Keyboard\", 59.99, 75),\n", |
| 1188 | + "]\n", |
| 1189 | + "\n", |
| 1190 | + "with sqlite3.connect(\"example.db\") as conn:\n", |
| 1191 | + " cursor = conn.cursor()\n", |
| 1192 | + " cursor.execute(\n", |
| 1193 | + " \"\"\"CREATE TABLE IF NOT EXISTS products \n", |
| 1194 | + " (id TEXT PRIMARY KEY, name TEXT, price REAL, stock INTEGER)\"\"\"\n", |
| 1195 | + " )\n", |
| 1196 | + " cursor.executemany(\n", |
| 1197 | + " \"\"\"INSERT OR REPLACE INTO products (id, name, price, stock) \n", |
| 1198 | + " VALUES (?, ?, ?, ?)\"\"\",\n", |
| 1199 | + " products_to_update,\n", |
| 1200 | + " )" |
| 1201 | + ] |
| 1202 | + }, |
| 1203 | + { |
| 1204 | + "cell_type": "code", |
| 1205 | + "execution_count": 16, |
| 1206 | + "id": "4a73a8c1-21ce-4531-9b5d-50c4b3ffc24b", |
| 1207 | + "metadata": {}, |
| 1208 | + "outputs": [ |
| 1209 | + { |
| 1210 | + "name": "stdout", |
| 1211 | + "output_type": "stream", |
| 1212 | + "text": [ |
| 1213 | + "P1={'name': 'Laptop', 'price': 999.99, 'stock': 50}\n", |
| 1214 | + "P2={'name': 'Mouse', 'price': 29.99, 'stock': 100}\n", |
| 1215 | + "P3={'name': 'Keyboard', 'price': 59.99, 'stock': 75}\n" |
| 1216 | + ] |
| 1217 | + } |
| 1218 | + ], |
| 1219 | + "source": [ |
| 1220 | + "with sqlite3.connect(\"example.db\") as conn:\n", |
| 1221 | + " cursor = conn.cursor()\n", |
| 1222 | + " cursor.execute(\"SELECT id, name, price, stock FROM products\")\n", |
| 1223 | + " for row in cursor.fetchall():\n", |
| 1224 | + " product_data = {\"name\": row[1], \"price\": row[2], \"stock\": row[3]}\n", |
| 1225 | + " print(f\"{row[0]}={product_data}\")" |
| 1226 | + ] |
| 1227 | + }, |
| 1228 | + { |
| 1229 | + "cell_type": "markdown", |
| 1230 | + "id": "546b3728-8e04-4d48-92b3-8927a86e2e6c", |
| 1231 | + "metadata": {}, |
| 1232 | + "source": [ |
| 1233 | + "You can use SqliteDict to handle all the SQL and serialization complexity with a familiar dictionary interface:\n", |
| 1234 | + "\n" |
| 1235 | + ] |
| 1236 | + }, |
| 1237 | + { |
| 1238 | + "cell_type": "code", |
| 1239 | + "execution_count": 17, |
| 1240 | + "id": "df941862-a501-4768-a1c9-43088cc4b7c2", |
| 1241 | + "metadata": {}, |
| 1242 | + "outputs": [], |
| 1243 | + "source": [ |
| 1244 | + "from sqlitedict import SqliteDict\n", |
| 1245 | + "\n", |
| 1246 | + "products_to_update = {\n", |
| 1247 | + " \"P1\": {\"name\": \"Laptop\", \"price\": 999.99, \"stock\": 50},\n", |
| 1248 | + " \"P2\": {\"name\": \"Mouse\", \"price\": 29.99, \"stock\": 100},\n", |
| 1249 | + " \"P3\": {\"name\": \"Keyboard\", \"price\": 59.99, \"stock\": 75},\n", |
| 1250 | + "}\n", |
| 1251 | + "\n", |
| 1252 | + "with SqliteDict(\"example2.db\") as db:\n", |
| 1253 | + " # Update multiple records in a batch\n", |
| 1254 | + " for product_id, product_data in products_to_update.items():\n", |
| 1255 | + " db[product_id] = product_data\n", |
| 1256 | + "\n", |
| 1257 | + " # Single commit for all updates\n", |
| 1258 | + " db.commit()" |
| 1259 | + ] |
| 1260 | + }, |
| 1261 | + { |
| 1262 | + "cell_type": "code", |
| 1263 | + "execution_count": 8, |
| 1264 | + "id": "21cc3d53-e01d-45ac-b46c-7d7f4e79a075", |
| 1265 | + "metadata": {}, |
| 1266 | + "outputs": [ |
| 1267 | + { |
| 1268 | + "name": "stdout", |
| 1269 | + "output_type": "stream", |
| 1270 | + "text": [ |
| 1271 | + "P1={'name': 'Laptop', 'price': 999.99, 'stock': 50}\n", |
| 1272 | + "P2={'name': 'Mouse', 'price': 29.99, 'stock': 100}\n", |
| 1273 | + "P3={'name': 'Keyboard', 'price': 59.99, 'stock': 75}\n" |
| 1274 | + ] |
| 1275 | + } |
| 1276 | + ], |
| 1277 | + "source": [ |
| 1278 | + "with SqliteDict(\"example2.db\") as db:\n", |
| 1279 | + " for key, item in db.items():\n", |
| 1280 | + " print(f\"{key}={item}\")" |
| 1281 | + ] |
| 1282 | + }, |
| 1283 | + { |
| 1284 | + "cell_type": "markdown", |
| 1285 | + "id": "8a5a4fd4-86cc-458f-89f2-2b2d027504d5", |
| 1286 | + "metadata": {}, |
| 1287 | + "source": [ |
| 1288 | + "The example shows how SqliteDict eliminates the need for explicit SQL statements, cursor management, and serialization. The tool handles schema creation, data type conversion, and connection management internally, while providing a Pythonic interface. This is particularly useful when you need to frequently store and retrieve complex Python objects without dealing with the underlying database complexity." |
| 1289 | + ] |
| 1290 | + }, |
| 1291 | + { |
| 1292 | + "cell_type": "markdown", |
| 1293 | + "id": "0b52e7ea-6b8b-4083-a1b4-5dac3da55783", |
| 1294 | + "metadata": {}, |
| 1295 | + "source": [ |
| 1296 | + "[Link to SqliteDict](https://github.com/piskvorky/sqlitedict)." |
| 1297 | + ] |
1156 | 1298 | }
|
1157 | 1299 | ],
|
1158 | 1300 | "metadata": {
|
|
0 commit comments