Feito o script

This commit is contained in:
Afonso Franco 2023-05-30 17:45:00 +01:00
parent 03c573a238
commit c0493d2bd5
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys
2 changed files with 57 additions and 0 deletions

36
csv2bfc.py Normal file
View file

@ -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()

21
exemplo.csv Normal file
View file

@ -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
1 cc email data_de_nascimento telefone nome morada codigo_postal cidade
2 71074967 Rita.Ferreira@mail.com 1969-08-02 480267895 Rita Ferreira Monserrate - Algarve 2847-040 Algarve
3 97502722 Antonio.Pessoa@mail.com 1983-10-18 290711973 Antonio Pessoa Monserrate - Guarda 7667-746 Guarda
4 45404168 Lalo.Sousa@mail.com 1967-02-16 219671598 Lalo Sousa Palmeira - Braga 0581-498 Braga
5 69972664 Ana.Alves@mail.com 1976-12-09 513351620 Ana Alves Areosa - Guarda 1257-599 Guarda
6 44835456 Rita.Silva@mail.com 1971-05-23 642760609 Rita Silva Palmeira - Funchal 1908-003 Funchal
7 31145010 Rita.Camacho@mail.com 1979-11-22 908498079 Rita Camacho Espinho - Aveiro 2094-568 Aveiro
8 72258814 Alberto.Caeiro@mail.com 1968-11-22 362259914 Alberto Caeiro Palmeira - Viana do Castelo 1603-599 Viana do Castelo
9 55030955 Antonio.Caeiro@mail.com 1944-05-31 191184006 Antonio Caeiro Espinho - Funchal 2301-374 Funchal
10 19500608 Alexandra.Ferreira@mail.com 1959-12-04 241420058 Alexandra Ferreira Palmeira - Braga 0470-358 Braga
11 40722004 Rita.Franco@mail.com 1956-11-12 569299345 Rita Franco Areosa - Algarve 5321-995 Algarve
12 72370006 Joao.Silva@mail.com 2003-04-20 712830254 Joao Silva Areosa - Algarve 3403-042 Algarve
13 24082638 Tiago.Fidalgo@mail.com 1986-02-24 601937335 Tiago Fidalgo Espinho - Mirandela 3938-149 Mirandela
14 66794687 Alexandra.Franco@mail.com 1984-04-18 291640102 Alexandra Franco Espinho - Viana do Castelo 3148-464 Viana do Castelo
15 68444011 Andre.Pessoa@mail.com 2005-08-17 644335737 Andre Pessoa Areosa - Aveiro 8789-816 Aveiro
16 63986902 Joao.Caeiro@mail.com 1980-04-26 117119687 Joao Caeiro Areosa - Viana do Castelo 9186-477 Viana do Castelo
17 76108120 Alexandra.Pessoa@mail.com 1982-06-07 543173493 Alexandra Pessoa Palmeira - Aveiro 5993-372 Aveiro
18 27990546 Andre.Fidalgo@mail.com 1950-12-15 508326457 Andre Fidalgo Monserrate - Viana do Castelo 5590-521 Viana do Castelo
19 79479190 Alexandra.Fidalgo@mail.com 1946-02-12 567230547 Alexandra Fidalgo Areosa - Aveiro 2376-294 Aveiro
20 69892293 Diogo.Pessoa@mail.com 2009-09-01 203577716 Diogo Pessoa Areosa - Lisboa 9070-250 Lisboa
21 31510785 Alzir.Fidalgo@mail.com 1969-11-07 641368139 Alzir Fidalgo Espinho - Guarda 7060-632 Guarda