· 6 years ago · Nov 16, 2019, 02:42 PM
1import sqlite3
2import logging
3import csv
4import sys
5import os.path
6import os
7
8
9path_csv=("./data.csv")
10path_db=("./batabase.sqlite3")
11path_log=("./logs.log")
12
13if not os.path.exists(path_csv):
14 raise Exception("%s doesn't exist" % path_csv)
15
16if not os.path.exists(path_db):
17 print("WARNING: Database %s doesn't exist; creating new db" % path_db)
18
19
20connection=sqlite3.connect(path_db)
21logging.basicConfig(filename=path_log,level=logging.INFO)
22cursor=connection.cursor()
23
24print("Creating table")
25
26cursor.execute('''
27 CREATE TABLE IF NOT EXISTS automobile(
28 adresse_titulaire text,
29 nom text,
30 prenom text,
31 immatriculation text,
32 date_immatriculation text,
33 vin text,
34 marque text,
35 denomination_commerciale text,
36 couleur text,
37 carosserie text,
38 categorie text,
39 cylindree int,
40 energie int,
41 places int,
42 poids int,
43 puissance int,
44 type text,
45 variante text,
46 version int
47 );
48 ''')
49connection.commit()
50
51
52
53with open(path_csv) as csvfile:
54 lecteur = csv.DictReader(csvfile, delimiter=';')
55 to_db = [(row['adresse_titulaire'],row['nom'],row['prenom'],row['immatriculation'],row['date_immatriculation'],row['vin'] ,row['marque'] ,row['denomination_commerciale'] ,row['couleur'] ,row['carosserie'],row['categorie'] ,row['cylindree'] ,row['energie'] ,row['places'],row['poids'],row['puissance'],row['type'] ,row['variante'] ,row['version'] ) for row in lecteur]
56
57cur.executemany("INSERT INTO automobiles ((adresse_titulaire,nom,prenom,immatriculation,date_immatriculation,vin,marque,denomination_commerciale,couleur,carosserie,categorie,cylindree,energie,places,poids,puissance,type,variante,version)) VALUES (?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", to_db)
58
59
60connection.commit()
61cursor.close()