Co-authored-by: José Henrique <jose.henrique.ivan@gmail.com>

This commit is contained in:
Gabriel Amaral 2023-12-02 22:44:33 -03:00
parent ccc961e5d0
commit fbecdd5af4
4 changed files with 185 additions and 5 deletions

View File

@ -11,7 +11,8 @@ def main():
escolha = input()
if escolha == "CSV":
print("Digite a pasta onde estão os arquivos CSV:")
print("Espere os arquivos.csv carregarem...")
#print("Digite a pasta onde estão os arquivos CSV:")
#csv_folder = input()
csv_folder = "./source"
@ -64,6 +65,7 @@ def main():
print('Carregando', table_name)
tables[table_name] = Table(table_name)
tables[table_name].load_from_mysql(table_name,cursor)
tables[table_name].save_to_csv()
print('Carregado', table_name)
print("\n")

View File

@ -1,4 +1,4 @@
city,state,country
London,England,United Kingdom
Kansas City,Kansas,United States
Rio de Janeiro,Rio de Janeiro,Brazil
city,state,country
London,England,United Kingdom
Kansas City,Kansas,United States
city,parana,goiania

1 city state country
2 London England United Kingdom
3 Kansas City Kansas United States
4 Rio de Janeiro city Rio de Janeiro parana Brazil goiania

155
sql.py
View File

@ -15,6 +15,12 @@ class SQL:
if query_parts[0] == "ISCOLHE":
self.select(query_parts)
elif query_parts[0] == "BOTAR":
self.insert(query_parts)
elif query_parts[0] == "SUMIR":
self.delete(query_parts)
elif query_parts[0] == "UPDEITA":
self.update(query_parts)
else:
pass
@ -93,3 +99,152 @@ class SQL:
row_values.append(str(row[column_index]))
print(', '.join(row_values))
# BOTAR tabela Coluna1,Coluna2,Coluna3 VALORES Valor1,Valor2,Valor3
def insert(self, query_parts: list) -> None:
insert_tabela = None
insert_colunas = []
insert_valores = []
index = query_parts.index("BOTAR")
insert_tabela = query_parts[index + 1]
insert_colunas = query_parts[index + 2].split(",")
insert_valores = query_parts[index + 4].split(",")
table = self.tables[insert_tabela]
row_value = []
for column_index in range(len(table.columns)):
column_name = table.columns[column_index]
try:
coluna_index = insert_colunas.index(column_name)
valor = self._try_parse_int(insert_valores[coluna_index])
except ValueError:
coluna_index = -1
valor = ""
row_value.append(valor)
table.rows.append(row_value)
table.save_to_csv()
# BOTAR tabela Coluna1,Coluna2,Coluna3 VALORES Valor1,Valor2,Valor3
def insert(self, query_parts: list) -> None:
insert_tabela = None
insert_colunas = []
insert_valores = []
index = query_parts.index("BOTAR")
insert_tabela = query_parts[index + 1]
insert_colunas = query_parts[index + 2].split(",")
insert_valores = query_parts[index + 4].split(",")
table = self.tables[insert_tabela]
row_value = []
for column_index in range(len(table.columns)):
column_name = table.columns[column_index]
try:
coluna_index = insert_colunas.index(column_name)
valor = self._try_parse_int(insert_valores[coluna_index])
except ValueError:
coluna_index = -1
valor = ""
row_value.append(valor)
table.rows.append(row_value)
table.save_to_csv()
# SUMIR tabela DONDE coluna = valor
def delete(self, query_parts: list) -> None:
delete_tabela = None
delete_coluna = None
delete_condicao = None
delete_valor = None
index = query_parts.index("SUMIR")
delete_tabela = query_parts[index + 1]
delete_coluna = query_parts[index + 3]
delete_condicao = query_parts[index + 4]
delete_valor = self._try_parse_int(query_parts[index + 5])
table = self.tables[delete_tabela]
for row in table.rows:
column_index = table.columns.index(delete_coluna)
if delete_condicao == '=' and row[column_index] == delete_valor:
table.rows.remove(row)
elif delete_condicao == '<' and row[column_index] < delete_valor:
table.rows.remove(row)
elif delete_condicao == '>' and row[column_index] > delete_valor:
table.rows.remove(row)
elif delete_condicao == '!=' and row[column_index] != delete_valor:
table.rows.remove(row)
table.save_to_csv()
# SUMIR tabela DONDE coluna = valor
def delete(self, query_parts: list) -> None:
delete_tabela = None
delete_coluna = None
delete_condicao = None
delete_valor = None
index = query_parts.index("SUMIR")
delete_tabela = query_parts[index + 1]
delete_coluna = query_parts[index + 3]
delete_condicao = query_parts[index + 4]
delete_valor = self._try_parse_int(query_parts[index + 5])
table = self.tables[delete_tabela]
column_index = table.columns.index(delete_coluna)
for row in table.rows:
if delete_condicao == '=' and row[column_index] == delete_valor:
table.rows.remove(row)
elif delete_condicao == '<' and row[column_index] < delete_valor:
table.rows.remove(row)
elif delete_condicao == '>' and row[column_index] > delete_valor:
table.rows.remove(row)
elif delete_condicao == '!=' and row[column_index] != delete_valor:
table.rows.remove(row)
table.save_to_csv()
# UPDEITA tabela coluna PARA valor DONDE coluna = valor
def update(self, query_parts: list) -> None:
update_tabela = None
update_coluna = None
update_valor = None
index = query_parts.index("UPDEITA")
update_tabela = query_parts[index + 1]
update_coluna = query_parts[index + 2]
update_valor = self._try_parse_int(query_parts[index + 4])
index = query_parts.index("DONDE")
update_where_coluna = query_parts[index + 1]
update_where_condicao = query_parts[index + 2]
update_where_valor = query_parts[index + 3]
table = self.tables[update_tabela]
update_col_index = table.columns.index(update_coluna)
where_col_index = table.columns.index(update_where_coluna)
for i in range(len(table.rows)):
if update_where_condicao == '=' and table.rows[i][where_col_index] == update_where_valor:
table.rows[i][update_col_index] = update_valor
elif update_where_condicao == '<' and table.rows[i][where_col_index] < update_where_valor:
table.rows[i][update_col_index] = update_valor
elif update_where_condicao == '>' and table.rows[i][where_col_index] > update_where_valor:
table.rows[i][update_col_index] = update_valor
elif update_where_condicao == '!=' and table.rows[i][where_col_index] != update_where_valor:
table.rows[i][update_col_index] = update_valor
table.save_to_csv()

View File

@ -57,6 +57,29 @@ class Table:
for row in columns:
self.columns.append(row[0])
def save_to_csv(self):
f = open("./source/" + self.name + ".csv", "w")
# gravar o cabeçalho
columns_line = ""
for i in range(len(self.columns)):
columns_line = columns_line + self.columns[i]
if i < len(self.columns) - 1:
columns_line = columns_line + ","
f.write(columns_line + '\n')
for line in range(len(self.rows)):
data_line = ""
for column in range(len(self.columns)):
data_line = data_line + str(self.rows[line][column])
if column < len(self.columns) - 1:
data_line = data_line + ","
f.write(data_line + '\n')
f.close()
def print_columns(self, column_list: list, where_filter: list = None, join_stmt: list = None):
if column_list[0] == '*':
column_list = self.columns