import mysql.connector
from mysql.connector import connect, Error
from getpass import getpass
nDB='Medicine'
PatientsCreate="""
CREATE TABLE IF NOT EXISTS Patients (
PatientsID INT PRIMARY KEY,
last_name VARCHAR(50),
first_name VARCHAR(50),
middle_name VARCHAR(50),
birthdate DATE,
gender VARCHAR(10) CHECK (gender IN ('Male', 'Female')),
address VARCHAR(100),
phone VARCHAR(20),
email VARCHAR(100)
);
"""
DoctorsCreate="""
CREATE TABLE IF NOT EXISTS Doctors (
DoctorsID INT PRIMARY KEY,
last_name VARCHAR(50),
first_name VARCHAR(50),
middle_name VARCHAR(50),
specialization INT NOT NULL,
phone VARCHAR(20),
email VARCHAR(100)
);
"""
Medical_servicesCreate="""
CREATE TABLE IF NOT EXISTS Medical_services (
Medical_servicesID INT PRIMARY KEY,
name_services VARCHAR(100),
description TEXT,
cost DECIMAL(10, 2)
);
"""
AppointmentsCreate="""
CREATE TABLE IF NOT EXISTS Appointments (
AppointmentsID INT PRIMARY KEY,
PatientID INT NOT NULL,
DoctorID INT NOT NULL,
Medical_servicesID INT NOT NULL,
datetime INT NOT NULL,
FOREIGN KEY (Patient_id) REFERENCES Customer (Patient_id),
FOREIGN KEY (Doctor_id) REFERENCES Customer (Doctor_id),
FOREIGN KEY (Medical_service_id) REFERENCES Customer (Medical_service_id)
);
"""
PatientsInsertMedicine = """
INSERT INTO Patients (PatientsID, last_name, first_name, middle_name, birthdate, gender, address, phone, email)
VALUES (1, 'Ivanov', 'Ivan', 'Ivanovich', '1980-01-01', 'Male', 'Moscow, Lenina street, 10', '+74951234567', 'ivanov@mail.com'),
(2, 'Petrova', 'Maria', 'Sergeevna', '1992-05-20', 'Female', 'St. Petersburg, Nevsky pr., 25', '+78124456789', 'petrova@mail.com'),
(3, 'Sidorov', 'Alexey', 'Igorevich', '1985-06-15', 'Male', 'Kazan, Pushkina street, 5', '+78435789876', 'sidorov@mail.com')
"""
DoctorsInsertMedicine = """
INSERT INTO Doctors (DoctorsID, last_name, first_name, middle_name, specialization, phone, email)
VALUES (1, 'Иванов', 'Иван', 'Иванович', 'Терапевт', '+7(999)999-99-99', 'ivanov@gmail.com'),
(2, 'Петров', 'Петр', 'Петрович', 'Хирург', '+7(999)999-99-98', 'petrov@gmail.com'),
(3, 'Сидоров', 'Сидор', 'Сидорович', 'Детский врач', '+7(999)999-99-97', 'sidorov@gmail.com')
"""
Medical_servicesInsertMedicine = """
INSERT INTO Medical_services (Medical_servicesID, name_services, description, cost)
VALUES (1, 'Прием врача', 'Консультация врача-специалиста', 2500.00),
(2, 'УЗИ органов брюшной полости', 'Ультразвуковое исследование органов брюшной полости', 5000.00),
(3, 'Анализ крови на биохимические показатели', 'Лабораторное исследование крови на биохимические показатели', 1500.00)
"""
AppointmentsInsertMedicine = """
INSERT INTO Appointments (AppointmentsID, PatientID, Medical_servicesID, datetime)
VALUES (1, '1', '1', '2023-10-20'),
(2, '2', '2', '2023-10-20'),
(3, '3', '3', '2023-10-20')
"""
# #процедуры
# CREATEPROCEDURE1="""
# CREATE PROCEDURE create_patient(IN patientID INT, IN last_name VARCHAR(50), IN first_name VARCHAR(50), IN middle_name VARCHAR(50), IN birthdate DATE, IN gender VARCHAR(10), IN address VARCHAR(100), IN phone VARCHAR(20), IN email VARCHAR(100))
# BEGIN
# INSERT INTO Patients (PatientID, last_name, first_name, middle_name, birthdate, gender, address, phone, email)
# VALUES (patientID, last_name, first_name, middle_name, birthdate, gender, address, phone, email);
# END
# """
# CREATEPROCEDURE2="""
# CREATE PROCEDURE create_doctor(IN doctorID INT, IN last_name VARCHAR(50), IN first_name VARCHAR(50), IN middle_name VARCHAR(50), IN specialization INT, IN phone VARCHAR(20), IN email VARCHAR(100))
# BEGIN
# INSERT INTO Doctors (DoctorID, last_name, first_name, middle_name, specialization, phone, email)
# VALUES (doctorID, last_name, first_name, middle_name, specialization, phone, email);
# END
# """
# #триггеры
# PatientsInsertTrigger = """
# CREATE TRIGGER insert_patients_address
# BEFORE INSERT ON Patients
# FOR EACH ROW
# BEGIN
# IF NEW.address IS NULL THEN
# SET NEW.address = 'Unknown address';
# END IF;
# END;
# """
# Medica_servicesDeleteTrigger = """
# CREATE TRIGGER delete_medical_services_history
# AFTER DELETE ON Medical_services
# FOR EACH ROW
# BEGIN
# DELETE FROM Patient_medical_record WHERE Medical_servicesID = OLD.Medical_servicesID;
# END;
# """
while True: # организуем циклическое выполнение задач
print ("""меню: 1 - ПОДКЛЮЧЕНИЕ, 2 - ПЕРЕЗАЛИВКА, 0 - ВЫХОД""")
Code = int(input('Ваш выбор '))
if Code==0 : # завершаем сеанс работы с БД
try:
cnx.close()
print ('сеанс работы с БД завершен')
except mysql.connector.Error as err:
print (err)
finally:
break
elif Code==1: # подключаемся к БД и создаем курсор
try:
cnx = mysql.connector.connect(user='root', password = '123', database= nDB)
cursor1= cnx.cursor()
print ('подключение к БД выполнено')
except mysql.connector.Error as err:
print (err)
elif Code==2:
try:
cursor1.execute("DROP TABLE IF EXISTS Appointments")
print ('таблица Appointments удалена')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute("DROP TABLE IF EXISTS Patients")
print ('таблица Patients удалена')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute("DROP TABLE IF EXISTS Doctors")
print ('таблица Doctors удалена')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute("DROP TABLE Medical_services")
print ('таблица Medical_services удалена')
except mysql.connector.Error as err:
print (err)
# создаем таблицы (важен порядок!)
try:
cursor1.execute(AppointmentsCreate)
print ('таблица Appointments создана')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(PatientsCreate)
print ('таблица Patients создана')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(DoctorsCreate)
print ('таблица Doctors создана')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(Medical_servicesCreate)
print ('таблица Medical_services создана')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(PatientsInsertMedicine)
cnx.commit()
print ('данные о Patients введены')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(DoctorsInsertMedicine)
cnx.commit()
print ('данные о Doctors введены')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(Medical_servicesInsertMedicine)
cnx.commit()
print ('данные о Medical_services введены')
except mysql.connector.Error as err:
print (err)
try:
cursor1.execute(AppointmentsInsertMedicine)
cnx.commit()
print ('данные о Appointments введены')
except mysql.connector.Error as err:
print (err)
print ('работа программы завершена')