Skip to content

Commit 39ede36

Browse files
committed
feat: last example on mysql
1 parent 0b7d435 commit 39ede36

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

notebooks/sn02_relational_databases.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,103 @@
610610
" cursor.close()\n",
611611
" conn.close()"
612612
]
613+
},
614+
{
615+
"cell_type": "code",
616+
"execution_count": 67,
617+
"id": "eeb321c2-e048-44e6-abb5-717a325d03cc",
618+
"metadata": {},
619+
"outputs": [
620+
{
621+
"name": "stderr",
622+
"output_type": "stream",
623+
"text": [
624+
"/tmp/ipykernel_28/3778911844.py:27: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n",
625+
" df_stocks = pd.read_sql(query, con=conn)\n"
626+
]
627+
},
628+
{
629+
"data": {
630+
"text/html": [
631+
"<div>\n",
632+
"<style scoped>\n",
633+
" .dataframe tbody tr th:only-of-type {\n",
634+
" vertical-align: middle;\n",
635+
" }\n",
636+
"\n",
637+
" .dataframe tbody tr th {\n",
638+
" vertical-align: top;\n",
639+
" }\n",
640+
"\n",
641+
" .dataframe thead th {\n",
642+
" text-align: right;\n",
643+
" }\n",
644+
"</style>\n",
645+
"<table border=\"1\" class=\"dataframe\">\n",
646+
" <thead>\n",
647+
" <tr style=\"text-align: right;\">\n",
648+
" <th></th>\n",
649+
" <th></th>\n",
650+
" <th>price</th>\n",
651+
" </tr>\n",
652+
" <tr>\n",
653+
" <th>ticker</th>\n",
654+
" <th>date</th>\n",
655+
" <th></th>\n",
656+
" </tr>\n",
657+
" </thead>\n",
658+
" <tbody>\n",
659+
" </tbody>\n",
660+
"</table>\n",
661+
"</div>"
662+
],
663+
"text/plain": [
664+
"Empty DataFrame\n",
665+
"Columns: [price]\n",
666+
"Index: []"
667+
]
668+
},
669+
"metadata": {},
670+
"output_type": "display_data"
671+
}
672+
],
673+
"source": [
674+
"import pandas as pd\n",
675+
"\n",
676+
"query = '''\n",
677+
"SELECT\n",
678+
"\ts.*\n",
679+
"FROM\n",
680+
"\tstocks AS s\n",
681+
"\tLEFT JOIN ( SELECT DISTINCT\n",
682+
"\t\t\t(ticker)\n",
683+
"\t\tFROM (\n",
684+
"\t\t\tSELECT\n",
685+
"\t\t\t\tprice / LAG(price) OVER (PARTITION BY ticker ORDER BY date) AS dif,\n",
686+
"\t\t\t\tticker\n",
687+
"\t\t\tFROM\n",
688+
"\t\t\t\tstocks) AS b\n",
689+
"\t\tWHERE\n",
690+
"\t\t\tdif < 0.99) AS a ON a.ticker = s.ticker\n",
691+
"WHERE\n",
692+
"\ta.ticker IS NULL\n",
693+
"'''\n",
694+
"\n",
695+
"try:\n",
696+
" conn = new_conn()\n",
697+
" cursor = conn.cursor()\n",
698+
"\n",
699+
"\n",
700+
" df_stocks = pd.read_sql(query, con=conn)\n",
701+
" df_stocks = df_stocks.set_index(['ticker', 'date'])\n",
702+
" display(df_stocks)\n",
703+
"except mysql.connector.Error as err:\n",
704+
" print('MySQL Error')\n",
705+
" print('(Code: {}) {}'.format(err.errno, err.msg))\n",
706+
"finally:\n",
707+
" cursor.close()\n",
708+
" conn.close()"
709+
]
613710
}
614711
],
615712
"metadata": {

0 commit comments

Comments
 (0)