{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "43fcf66c", "metadata": {}, "outputs": [], "source": [ "import sqlite3\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 3, "id": "a1b60ba1", "metadata": {}, "outputs": [], "source": [ "sqc = sqlite3.connect( \"chinook.db\" )" ] }, { "cell_type": "code", "execution_count": 4, "id": "a9647e2c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] }, { "data": { "text/plain": [ "\"SELECT * FROM tracks WHERE Name = 'hello';\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = input()\n", "query = f\"SELECT * FROM tracks WHERE Name = '{name}';\"\n", "query" ] }, { "cell_type": "code", "execution_count": 11, "id": "a35432f3", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'NoneType' object is not iterable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[11], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m cursor \u001b[38;5;241m=\u001b[39m sqc\u001b[38;5;241m.\u001b[39mexecutescript( query )\n\u001b[1;32m----> 2\u001b[0m cols \u001b[38;5;241m=\u001b[39m [ col[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;28;01mfor\u001b[39;00m col \u001b[38;5;129;01min\u001b[39;00m cursor\u001b[38;5;241m.\u001b[39mdescription ]\n\u001b[0;32m 3\u001b[0m df \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame\u001b[38;5;241m.\u001b[39mfrom_records( cursor, columns \u001b[38;5;241m=\u001b[39m cols )\n\u001b[0;32m 4\u001b[0m df\u001b[38;5;241m.\u001b[39mhead()\n", "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object is not iterable" ] } ], "source": [ "cursor = sqc.executescript( query )\n", "cols = [ col[0] for col in cursor.description ]\n", "df = pd.DataFrame.from_records( cursor, columns = cols )\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 16, "id": "c35e0b0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{':tname': \"'; DROP TABLE genres; --\"}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pdict" ] }, { "cell_type": "code", "execution_count": 12, "id": "cf0708ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'; DROP TABLE genres; --\n" ] }, { "data": { "text/plain": [ "'SELECT * FROM tracks WHERE Name = :tname;'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = input()\n", "query = f\"SELECT * FROM tracks WHERE Name = :tname;\"\n", "query" ] }, { "cell_type": "code", "execution_count": 17, "id": "e0a8d2f2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TrackIdNameAlbumIdMediaTypeIdGenreIdComposerMillisecondsBytesUnitPrice
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [TrackId, Name, AlbumId, MediaTypeId, GenreId, Composer, Milliseconds, Bytes, UnitPrice]\n", "Index: []" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pdict = { 'tname' : name }\n", "cursor = sqc.execute( query, pdict )\n", "cols = [ col[0] for col in cursor.description ]\n", "df = pd.DataFrame.from_records( cursor, columns = cols )\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 75, "id": "6327d242", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"('AC/DC', 'Apocalyptica')\"" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"('\"+\"', '\".join(vals)+\"')\"" ] }, { "cell_type": "code", "execution_count": 77, "id": "51004fcd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TrackIdNameAlbumIdMediaTypeIdGenreIdComposerMillisecondsBytesUnitPrice
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [TrackId, Name, AlbumId, MediaTypeId, GenreId, Composer, Milliseconds, Bytes, UnitPrice]\n", "Index: []" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"SELECT * FROM tracks WHERE Composer IN (?);\"\n", "vals = [ \"AC/DC\", \"Apocalyptica\" ]\n", "params = \"'\"+\"', '\".join(vals)+\"'\"\n", "cursor = sqc.execute( query, ((params,)) )\n", "cols = [ col[0] for col in cursor.description ]\n", "df = pd.DataFrame.from_records( cursor, columns = cols )\n", "df.head(100)" ] }, { "cell_type": "markdown", "id": "fa5d0565", "metadata": {}, "source": [ "https://stackoverflow.com/questions/1309989/parameter-substitution-for-a-sqlite-in-clause" ] }, { "cell_type": "code", "execution_count": 11, "id": "b8db4a71", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':p0, :p1'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"{}\".format(\", \".join( \":p{}\".format(str(i)) for i in range(len(vals)) ))" ] }, { "cell_type": "code", "execution_count": 20, "id": "fa362dbf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'p0': 'AC/DC', 'p1': 'Apocalyptica'}\n" ] } ], "source": [ "params = {}\n", "params.update( { 'p' + str(i) : vals[i] for i, val in enumerate(vals) } )\n", "print( params )" ] }, { "cell_type": "code", "execution_count": null, "id": "6bea061f", "metadata": {}, "outputs": [], "source": [ "res = cursor.fetchall()\n", "res" ] }, { "cell_type": "code", "execution_count": null, "id": "8be4eba8", "metadata": {}, "outputs": [], "source": [ "sqc.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" } }, "nbformat": 4, "nbformat_minor": 5 }