This commit is contained in:
Gabriel Amaral 2023-11-30 20:15:30 -03:00
parent ddf8412747
commit a08fd5f98b
2 changed files with 23 additions and 8 deletions

20
main.py
View File

@ -3,6 +3,7 @@ import os
from table import Table from table import Table
from sql import SQL from sql import SQL
import mysql.connector import mysql.connector
from mysql.connector import Error
def main(): def main():
print("Bem-vindo!") print("Bem-vindo!")
@ -40,7 +41,24 @@ def main():
print() print()
elif escolha == "BD": elif escolha == "BD":
print("BD") try:
connection = mysql.connector.connect(host='localhost',
database='employees',
user='employeesdb',
password='Employees1!')
if connection.is_connected():
print("Conectado ao employees-db")
cursor = connection.cursor()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
else: else:
print("Fora do intervalo!") print("Fora do intervalo!")

11
sql.py
View File

@ -1,13 +1,8 @@
from table import Table
class SQL: class SQL:
def __init__(self, tables: dict, query: str) -> None: def __init__(self, tables: dict, query: str) -> None:
self.tables = tables self.tables = tables
self.query = query.replace(";", "") self.query = query.replace(";", "")
def __str__(self) -> str:
pass
# ISCOLHE * DE tabela # ISCOLHE * DE tabela
def execute(self): def execute(self):
query_parts = self.query.split(" ") query_parts = self.query.split(" ")
@ -17,7 +12,8 @@ class SQL:
else: else:
pass pass
# ISCOLHE * DE tabela AJUNTAR tabela2 coluna DONDE coluna = valor # ISCOLHE * DE tabela AJUNTAR tabela2 EM coluna;
# ISCOLHE * DE tabela DONDE coluna = valor;
def select(self, query_parts: list) -> None: def select(self, query_parts: list) -> None:
where_filter = None where_filter = None
join_stmt = None join_stmt = None
@ -25,7 +21,8 @@ class SQL:
if "AJUNTAR" in query_parts: if "AJUNTAR" in query_parts:
index = query_parts.index("AJUNTAR") index = query_parts.index("AJUNTAR")
join_table = query_parts[index + 1] join_table = query_parts[index + 1]
join_column = query_parts[index + 2] join_operator = query_parts[index + 2] # EM -> estetico
join_column = query_parts[index + 3]
join_stmt = [self.tables[join_table], join_column] join_stmt = [self.tables[join_table], join_column]
if "DONDE" in query_parts: if "DONDE" in query_parts: