From c0493d2bd5c3877b7d3735bae0b57da00266f2fd Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Tue, 30 May 2023 17:45:00 +0100 Subject: [PATCH] Feito o script --- csv2bfc.py | 36 ++++++++++++++++++++++++++++++++++++ exemplo.csv | 21 +++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 csv2bfc.py create mode 100644 exemplo.csv diff --git a/csv2bfc.py b/csv2bfc.py new file mode 100644 index 0000000..517e657 --- /dev/null +++ b/csv2bfc.py @@ -0,0 +1,36 @@ +import sys,csv +import mysql.connector + +def main(): + + connection = mysql.connector.connect(user='root', password='123', + host='127.0.0.1', + database='biblioteca_fc') + + file = open(sys.argv[1],"r") + lines = csv.reader(file) + + #Get name and order of columns + columns = next(lines) + + #Replace empty values with null + values = [[value if value else None for value in line] for line in lines] + + #Create placeholder strings + placeholders = ', '.join(['%s'] * len(columns)) + + #Create query + query = f"INSERT INTO Utilizador ({', '.join(columns)}) VALUES ({placeholders})" + + #Execute query + cursor = connection.cursor() + cursor.executemany(query,values) + connection.commit() + + #Close everything + file.close() + cursor.close() + connection.close() + +if __name__ == "__main__": + main() diff --git a/exemplo.csv b/exemplo.csv new file mode 100644 index 0000000..16f91c9 --- /dev/null +++ b/exemplo.csv @@ -0,0 +1,21 @@ +cc,email,data_de_nascimento,telefone,nome,morada,codigo_postal,cidade +71074967,Rita.Ferreira@mail.com,1969-08-02,480267895,Rita Ferreira,Monserrate - Algarve,2847-040,Algarve +97502722,Antonio.Pessoa@mail.com,1983-10-18,290711973,Antonio Pessoa,Monserrate - Guarda,7667-746,Guarda +45404168,Lalo.Sousa@mail.com,1967-02-16,219671598,Lalo Sousa,Palmeira - Braga,0581-498,Braga +69972664,Ana.Alves@mail.com,1976-12-09,513351620,Ana Alves,Areosa - Guarda,1257-599,Guarda +44835456,Rita.Silva@mail.com,1971-05-23,642760609,Rita Silva,Palmeira - Funchal,1908-003,Funchal +31145010,Rita.Camacho@mail.com,1979-11-22,908498079,Rita Camacho,Espinho - Aveiro,2094-568,Aveiro +72258814,Alberto.Caeiro@mail.com,1968-11-22,362259914,Alberto Caeiro,Palmeira - Viana do Castelo,1603-599,Viana do Castelo +55030955,Antonio.Caeiro@mail.com,1944-05-31,191184006,Antonio Caeiro,Espinho - Funchal,2301-374,Funchal +19500608,Alexandra.Ferreira@mail.com,1959-12-04,241420058,Alexandra Ferreira,Palmeira - Braga,0470-358,Braga +40722004,Rita.Franco@mail.com,1956-11-12,569299345,Rita Franco,Areosa - Algarve,5321-995,Algarve +72370006,Joao.Silva@mail.com,2003-04-20,712830254,Joao Silva,Areosa - Algarve,3403-042,Algarve +24082638,Tiago.Fidalgo@mail.com,1986-02-24,601937335,Tiago Fidalgo,Espinho - Mirandela,3938-149,Mirandela +66794687,Alexandra.Franco@mail.com,1984-04-18,291640102,Alexandra Franco,Espinho - Viana do Castelo,3148-464,Viana do Castelo +68444011,Andre.Pessoa@mail.com,2005-08-17,644335737,Andre Pessoa,Areosa - Aveiro,8789-816,Aveiro +63986902,Joao.Caeiro@mail.com,1980-04-26,117119687,Joao Caeiro,Areosa - Viana do Castelo,9186-477,Viana do Castelo +76108120,Alexandra.Pessoa@mail.com,1982-06-07,543173493,Alexandra Pessoa,Palmeira - Aveiro,5993-372,Aveiro +27990546,Andre.Fidalgo@mail.com,1950-12-15,508326457,Andre Fidalgo,Monserrate - Viana do Castelo,5590-521,Viana do Castelo +79479190,Alexandra.Fidalgo@mail.com,1946-02-12,567230547,Alexandra Fidalgo,Areosa - Aveiro,2376-294,Aveiro +69892293,Diogo.Pessoa@mail.com,2009-09-01,203577716,Diogo Pessoa,Areosa - Lisboa,9070-250,Lisboa +31510785,Alzir.Fidalgo@mail.com,1969-11-07,641368139,Alzir Fidalgo,Espinho - Guarda,7060-632,Guarda