I made a code allowing me to GET data from an API and insert it into a database. But I encounter a problem my api contains about 20 million data and to insert everything in my database it will take me 43 days :)
I think the problem comes from the fact that I insert the data one by one.
That’s why I’m looking for how to execute all my queries at once.
The problem is that I get my data as a list[dict] like that :
JavaScript
x
14
14
1
<class 'list'>
2
{
3
'siren': 'siren_int',
4
'nic': 'nic int',
5
'dateCreationEtablissement': '2000-09-26',
6
'uniteLegale': {'denominationUniteLegale': None, 'nomUniteLegale': 'last name', 'prenomUsuelUniteLegale': 'firstname'},
7
'adresseEtablissement': {
8
'numeroVoieEtablissement': None,
9
'libelleVoieEtablissement': 'street name',
10
'codePostalEtablissement': 'postal code',
11
'libelleCommuneEtablissement': 'commune name',
12
}
13
}
14
Currently my code looks like this :
JavaScript
1
10
10
1
def insertGlobalBdd(self, rows : list[dict]) :
2
cursor = self.bdd.cursor()
3
for element in rows:
4
rows.append((element["siren"], self.TestFormat(element["uniteLegale"]["nomUniteLegale"]),
5
self.TestFormat(element["uniteLegale"]["prenomUsuelUniteLegale"]), self.TestFormat(element["uniteLegale"]["denominationUniteLegale"]),
6
element["dateCreationEtablissement"]))
7
8
cursor.execute("""INSERT INTO %s.dbo.Entreprise (siren, nomUniteLegale, prenomUsuelUniteLegale, denominationuniteLegale, dateCreationEtablissement) VALUES ('%s', '%s', '%s', '%s', '%s')""".format(",".join(str(i) for i in rows)))
9
self.bdd.commit()
10
would you have any idea how to do it?
Advertisement
Answer
You can do with ‘executemany’.
example
JavaScript
1
9
1
task_all[
2
(1, "Jahanzabe", 30),
3
(2, "Steve", 20),
4
(3, "Haider", 30),
5
(All Others )] .
6
cur.executemany('Insert in to Table_name(id, name, no)
7
values(?,?,?)', task_all)
8
con.commit()
9