-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44e427c
commit 1fe5915
Showing
5 changed files
with
417 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pip install --upgrade pip | ||
pip install mysqlclient | ||
sudo pip install mysqlclient | ||
cd /etc/ | ||
ls | ||
cat *release* |
325 changes: 325 additions & 0 deletions
325
databases/python/.ipynb_checkpoints/mysql_py-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,325 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2df03dd8-0636-4057-8cbe-6e8ef53d59d9", | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install mysql-connector-python\n", | ||
"!pip install sqlalchemy\n", | ||
"!pip install PyMySQL\n", | ||
"!pip install mysqlclient\n", | ||
"!pip install configparser" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "64a65b56-4a2e-4dff-8ebc-a462629ebee1", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from mysql.connector import connect, Error\n", | ||
"from getpass import getpass\n", | ||
"\n", | ||
"try:\n", | ||
" with connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=input(\"Enter username: \"),\n", | ||
" password=getpass(\"Enter password: \"),\n", | ||
" ) as connection:\n", | ||
" create_db_query = \"CREATE DATABASE testdb;\"\n", | ||
" with connection.cursor() as cursor:\n", | ||
" cursor.execute(create_db_query)\n", | ||
"except Error as e:\n", | ||
" print(e)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e942ae2b-a12c-4d87-90b3-13efb55645da", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from mysql.connector import connect, Error" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "347927a0-bd5c-42c7-b7e0-6ffadfdb1bae", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" buffered=True\n", | ||
")\n", | ||
"\n", | ||
"print(mysqldb)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5f003bef-64ac-4882-8389-d0e4eb1f5582", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" buffered=True\n", | ||
")\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"\n", | ||
"mycursor.execute(\"SHOW DATABASES;\")\n", | ||
"print( mycursor.fetchall())\n", | ||
"\n", | ||
"#for db in dbs:\n", | ||
"# print (db)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6fce2f16-cecd-40e5-a68e-51acbc45b352", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" buffered=True\n", | ||
")\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"\n", | ||
"mycursor.execute(\"CREATE DATABASE IF NOT EXISTS testdb;\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "abc1fcb4-3d0f-424b-a3f4-49906ac9ccbd", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" database=\"testdb\"\n", | ||
")\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"\n", | ||
"mycursor.execute(\"SHOW DATABASES;\")\n", | ||
"print( mycursor.fetchall())\n", | ||
"\n", | ||
"qry = \"\"\"\n", | ||
"USE testdb;\n", | ||
"CREATE TABLE t1 (id INT, first_name VARCHAR(20));\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"mycursor.execute(qry)\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "92c72faa-e76f-4039-af6d-f864cf80d6e7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" database=\"testdb\"\n", | ||
")\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"qry = \"\"\"\n", | ||
"-- insert values into table t1\n", | ||
"INSERT INTO t1 VALUES (1, 'ahmed'), (2, 'aya'), (3, 'john');\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"mycursor.execute(qry)\n", | ||
"mysqldb.commit()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a0f324e5-aa5b-4095-9e30-c9146993f5d6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" database=\"testdb\"\n", | ||
")\n", | ||
"\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"\n", | ||
"qry = \"DESC t1\"\n", | ||
"\n", | ||
"mycursor.execute(qry)\n", | ||
"result = mycursor.fetchall()\n", | ||
"\n", | ||
"#print (result)\n", | ||
"for res in result:\n", | ||
" print(res)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7137d501-f3e2-4279-b0a5-48cdd7aaec50", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mysqldb = connect(\n", | ||
" host=\"mysql-db\",\n", | ||
" user=\"root\",\n", | ||
" password=\"P@ssw0rd\",\n", | ||
" database=\"testdb\"\n", | ||
")\n", | ||
"\n", | ||
"\n", | ||
"mycursor = mysqldb.cursor()\n", | ||
"\n", | ||
"qry = \"SELECT * FROM t1\"\n", | ||
"\n", | ||
"mycursor.execute(qry)\n", | ||
"result = mycursor.fetchall()\n", | ||
"\n", | ||
"print (result)\n", | ||
"#for res in result:\n", | ||
"# print(res)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0c69a2c8-9a6f-40fe-b592-09cb739c50e2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mycursor.close()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "51c8a8a6-bde8-40ef-ae52-646f34f3adba", | ||
"metadata": {}, | ||
"source": [ | ||
"## SQLAlchemy" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "21bfbd6d-f8e1-4e6a-86e8-72c0fa628f55", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sqlalchemy import create_engine, text\n", | ||
"#import MySQLdb" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9c599431-bd0f-40b8-88b8-f3259ec0933c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"connection_string = \"mysql+mysqldb://root:P%40ssw0rd@mysql-db:3306\"\n", | ||
"engine = create_engine(connection_string, echo=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "de246924-2dc7-400b-ae8b-0c7e8e09cace", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"conn = engine.connect()\n", | ||
"#conn.execute(\"CREATE DATABASE testdb;\")\n", | ||
"results = conn.execute(text(\"SHOW DATABASES;\"))\n", | ||
"\n", | ||
"for result in results:\n", | ||
" print(result)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e346f021-cbd9-4de9-b727-11596105f4dc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"results = conn.execute(text(\"CREATE DATABASE testdb;\"))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "cdf3438e-ccbb-4837-9cea-406db41f7d46", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"results = conn.execute(text(\"SHOW DATABASES;\"))\n", | ||
"\n", | ||
"for result in results:\n", | ||
" print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9671f09d-911f-4a1f-91d9-6e296fb6fc57", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.11.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.