bonjour

j'ai un problème avec le processus d'insertion des donnée d'une zone de texte contenant une ou plusieurs apostrophes.


lancerv1.py
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
 
# coding: utf-8  
'''
Created on 11 déc. 2017
 
 @author: eric
'''
import string
import sys
import datetime
import csv
import os
 
 
 
from Tables import *
from Explorateur import *
from base import *
 
 
if __name__ == '__main__':
    nom_fichier_path_in = explorateur_window (".//") # Recherche via explorateur
    if os.path.isfile(nom_fichier_path_in):
 
        # Ouverture du fichier destination
        nom_fichier_in = os.path.basename(nom_fichier_path_in) # Retourne le nom du fichier
        nom_fichier_path = os.path.dirname(nom_fichier_path_in) # Retourne le répertoire complet
        list_nf = os.path.split(nom_fichier_path_in) # Fractionne le chemin d'acc�s. Retourne un 
        projet = Creation_table()  # Base de données
        projet.nomrepertoire= nom_fichier_path
        projet.creer_table()
        print(projet.dbNAME)      
 
 
        fic = exercice()
        fic.libelle ="regarde l'endive"
        fic.numero =2
        fic.montant = 26.2
        print(fic)
        fic.db_insertion(projet)
 
        fic.libelle ="L'homme est vivant "
        fic.numero =3
        fic.montant = 8.25
        print(fic)
        fic.db_insertion(projet)
 
        fic.libelle ="D'or de d'argent"
        fic.numero = 4
        fic.montant = 9879.15
        print(fic)
        fic.db_insertion(projet)
 
        fic.libelle ="4 millions d'euros"
        fic.numero = 5
        fic.montant = 56
        print(fic)
        fic.db_insertion(projet)
        fic.db_insertion(projet)
Tables.py

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
# coding: utf-8  
import sqlite3
import os
 
 
class Creation_table(object):
 
    """ Structure des tables  """
 
    def __init__(self):
        self.dbNAME ='DB_primer.sq3'  
        self.nomrepertoire =None
        self.Base =None
 
    def creer_table(self):
      # Supression du fichire des la base et reconception
 
        self.dbNAME =self.nomrepertoire+'/'  +self.dbNAME
 
         # print("Lancer de la base SQLite:"+self.dbNAME )
        if(os.path.isfile(self.dbNAME)== True): #Cette méthode retourne true si le chemin spécifié 
            os.remove(self.dbNAME) # pour supprimer un fichier 
 
 
        self.Base =sqlite3.connect(self.dbNAME)  # ouverture DB
 
        curseur = self.Base.cursor() # appel au curseur
 
        table ="""CREATE TABLE Produit_0110 (
        numero INETGER,
        libelle nvarchar(150),
        montant  real);"""
 
        if(table!=""):
            try:
                curseur.execute(table)
                self.Base.commit()
            except:
                print("Problème table")  
 
        self.Base.close()
base.py

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
 
 
# coding: utf-8  
from Tables import *
'''
Created on 11 déc. 2017
 
@author: eric
'''
from builtins import str
 
class exercice(object):
    '''
    classdocs
    '''
 
    def __init__(self):
        '''
           CREATE TABLE exercice (
            numero INTEGER,
            libelle nvarchar(100),
            montant real);
 
        '''
        self.numero= None
        self.libelle =None
        self.montant = None
 
    def __str__(self):
        resultat = 'numero :' + str(self.numero) +'\n'
        resultat += 'libelle : ' + self.libelle +'\n'
        resultat += 'montant : '+str(self.montant)
        return resultat
 
 
    def db_insertion(self,nombase):
 
        rq = "INSERT INTO Produit_0110"
        rq += " (numero, libelle, montant)"
        rq +="  VALUES(" + str( self.numero) + ","
        rq += "'" + self.libelle + "',"
        rq += str( self.montant) + ")"
        print(rq)
        try:
            #print(nombase.dbNAME)
 
            nombase =sqlite3.connect(nombase.dbNAME)  # ouverture DB
            try:
                curseur = nombase.cursor() # appel au curseur
                try:
                    curseur.execute(rq)
                except:
                    print("Insertion hs Produit_0110")
                nombase.commit()
            except:
                print("problème de curseur")
            nombase.close()   
        except:
            print("acces Base impossible")

le problème des insert est le suivant

voici le résultat du print(rq)

numero :2
libelle : regarde l'endive
montant : 26.2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
INSERT INTO Produit_0110 (numero, libelle, montant)  VALUES(2,'regarde l'endive',26.2)
Insertion hs Produit_0110




j'ai cherché une autre solution

base.py
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
# coding: utf-8  
from Tables import *
'''
Created on 11 déc. 2017
 
@author: eric
'''
from builtins import str
 
class exercice(object):
    '''
    classdocs
    '''
 
    def __init__(self):
        '''
           CREATE TABLE exercice (
            numero INTEGER,
            libelle nvarchar(100),
            montant real);
 
        '''
        self.numero= None
        self.libelle =None
        self.montant = None
 
    def __str__(self):
        resultat = 'numero :' + str(self.numero) +'\n'
        resultat += 'libelle : ' + self.libelle +'\n'
        resultat += 'montant : '+str(self.montant)
        return resultat
 
    def __csv_donnee__(self):
        resulta = [self.numero, self.libelle,self.montant]
        return resulta
    pass
 
    def db_insertion(self,nombase):
 
        rq = "INSERT INTO Produit_0110"
        rq += " (numero, libelle, montant)"
        rq += "  VALUES(?,?,?)"
 
        try:
            #print(nombase.dbNAME)
 
            nombase =sqlite3.connect(nombase.dbNAME)  # ouverture DB
            try:
                curseur = nombase.cursor() # appel au curseur
                try:
                    curseur.execute(rq ,  __csv_donnee__()) 
                except:
                    print("Insertion hs Produit_0110")
                nombase.commit()
            except:
                print("problème de curseur")
            nombase.close()   
        except:
            print("acces Base impossible")
malheureusement


numero :2
libelle : regarde l'endive
montant : 26.2
Insertion hs Produit_0110
numero :3
libelle : L'homme est vivant
montant : 8.25
Insertion hs Produit_0110
numero :4
libelle : D'or de d'argent
montant : 9879.15
Insertion hs Produit_0110
numero :5
libelle : 4 millions d'euros
montant : 56
Insertion hs Produit_0110
Insertion hs Produit_0110

base.py (version3) et cette version marche sans prolème mais si vous avez des idees d'amélioration

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
60
61
62
63
64
65
66
67
# coding: utf-8  
from Tables import *
'''
Created on 11 déc. 2017
 
@author: eric
'''
from builtins import str
 
class exercice(object):
    '''
    classdocs
    '''
 
    def __init__(self):
        '''
           CREATE TABLE exercice (
            numero INTEGER,
            libelle nvarchar(100),
            montant real);
 
        '''
        self.numero= None
        self.libelle =None
        self.montant = None
 
    def __str__(self):
        resultat = 'numero :' + str(self.numero) +'\n'
        resultat += 'libelle : ' + self.libelle +'\n'
        resultat += 'montant : '+str(self.montant)
        return resultat
 
    def __csv_donnee__(self):
        resulta = [self.numero, self.libelle,self.montant]
        return resulta
    pass
 
    def liste_donnee(self):
        resulta = (self.numero, self.libelle,self.montant)
        return resulta
    pass
 
    pass
 
 
    def db_insertion(self,nombase):
 
        rq = "INSERT INTO Produit_0110"
        rq += " (numero, libelle, montant)"
        rq +="  VALUES(?,?,?)"
 
        try:
            #print(nombase.dbNAME)
 
            nombase =sqlite3.connect(nombase.dbNAME)  # ouverture DB
            try:
                curseur = nombase.cursor() # appel au curseur
                try:
                    curseur.execute(rq ,  self.liste_donnee()) 
                except:
                    print("Insertion hs Produit_0110")
                nombase.commit()
            except:
                print("problème de curseur")
            nombase.close()   
        except:
            print("acces Base impossible")