1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
LstChamps=""
LstValues=""
TblValues={}
#---- Maintenant, on parcours les colonnes qui ont une correspondance SQL
for IndCol in range(len(self.TblCorresp)) :
if LstChamps!="":
LstChamps+=", "
LstValues+=", "
NoColFic=self.TblCorresp[IndCol]["NOCOL"]
NomChamp=self.TblCorresp[IndCol]["NOMCHAMP"]
LstChamps+=NomChamp
if (NoColFic>len(self.TblFicCSV[NoLigne])):
TblValues[NomChamp]=""
else:
TblCharset=chardet.detect(self.TblFicCSV[NoLigne][NoColFic])
if TblCharset["encoding"]=="ascii" or TblCharset["encoding"]==None :
TblValues[NomChamp]=self.TblFicCSV[NoLigne][NoColFic]
else :
print "-----------------------------------------"
print self.TblFicCSV[NoLigne][NoColFic]
print TblCharset["encoding"]
print unicode(self.TblFicCSV[NoLigne][NoColFic].decode(TblCharset["encoding"]))
TblValues[NomChamp]=unicode(self.TblFicCSV[NoLigne][NoColFic].decode(TblCharset["encoding"]))
#---- Formatage des champs en fonction de leur type
if self.TblCorresp[IndCol]["TYPE"]=="char" or self.TblCorresp[IndCol]["TYPE"]=="varchar" or self.TblCorresp[IndCol]["TYPE"]=="nchar" or self.TblCorresp[IndCol]["TYPE"]=="nvarchar" or self.TblCorresp[IndCol]["TYPE"]=="text":
#---- Chaîne
TblValues[NomChamp]=self.FormatSql(TblValues[NomChamp], "C")
LstValues+="'{"+NomChamp+"}'"
elif self.TblCorresp[IndCol]["TYPE"]=="datetime" :
#---- Date
TblValues[NomChamp]=self.FormatSql(TblValues[NomChamp], "D")
LstValues+="'{"+NomChamp+"}'"
elif self.TblCorresp[IndCol]["TYPE"]=="decimal" :
#---- Réel
TblValues[NomChamp]=self.FormatSql(TblValues[NomChamp], "R")
LstValues+="{"+NomChamp+"}"
else :
TblValues[NomChamp]=self.FormatSql(TblValues[NomChamp], "E")
LstValues+="{"+NomChamp+"}"
Requete="INSERT INTO "+self.NomTable+" ("+LstChamps+") VALUES ("+LstValues+")"
Requete=unicode(Requete)
try:
cur = self.CnxBase.cursor()
cur.execute(Requete.format(**TblValues))
self.CnxBase.commit()
except db.ProgrammingError:
print "--------- Erreur dans "+self.FichierCourant+" ligne "+str(NoLigne)+"-------------------"
print Requete.format(**TblValues) |