Created
March 15, 2023 23:09
-
-
Save Da9el00/4e33eee28d9a1f153674bed34899ea82 to your computer and use it in GitHub Desktop.
python and MySQL
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
use db; | |
CREATE TABLE students( | |
StudentID int not null AUTO_INCREMENT, | |
FirstName varchar(100) NOT NULL, | |
Surname varchar(100) NOT NULL, | |
PRIMARY KEY (StudentID) | |
); | |
INSERT INTO students(FirstName, Surname) | |
VALUES("John", "Andersen"), ("Emma", "Smith"); |
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
services: | |
pythonapp: | |
build: ./python/ | |
command: sh -c "sleep 10s ; python3 ./hello_world.py" | |
depends_on: | |
- mysql | |
mysql: | |
build: ./mysql/ | |
restart: always | |
environment: | |
MYSQL_DATABASE: 'db' | |
MYSQL_ROOT_PASSWORD: 'root' | |
ports: | |
- '3306:3306' |
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
# NOTE name need to be Dockerfile | |
FROM mysql:latest | |
COPY ./databse_students.sql /docker-entrypoint-initdb.d/ |
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
# NOTE name need to be Dockerfile | |
FROM python:3.9 | |
RUN pip install mysql-connector-python | |
WORKDIR /usr/app/src | |
COPY hello_world.py ./ |
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
import mysql.connector | |
connection = mysql.connector.connect( | |
user='root', password='root', host='mysql', port="3306", database='db') | |
print("DB connected") | |
cursor = connection.cursor() | |
cursor.execute('Select * FROM students') | |
students = cursor.fetchall() | |
connection.close() | |
print(students) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment