Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created March 15, 2023 23:09
Show Gist options
  • Save Da9el00/4e33eee28d9a1f153674bed34899ea82 to your computer and use it in GitHub Desktop.
Save Da9el00/4e33eee28d9a1f153674bed34899ea82 to your computer and use it in GitHub Desktop.
python and MySQL
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");
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'
# NOTE name need to be Dockerfile
FROM mysql:latest
COPY ./databse_students.sql /docker-entrypoint-initdb.d/
# NOTE name need to be Dockerfile
FROM python:3.9
RUN pip install mysql-connector-python
WORKDIR /usr/app/src
COPY hello_world.py ./
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