IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Base de données SQLite


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mai 2017
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2017
    Messages : 12
    Par défaut Base de données SQLite
    Bonjour,
    je poste ici car après avoir suivi un tuto à la lettre j'ai un souci avec la base de données pour mon application.

    Le plus gros problème c'est que tout compile sans erreur, mais que les données ne s'insèrent pas dans la table.
    Je ne comprends pas d'où vient le problème.
    J'ai essayé de me servir de adb mais je n'arrive pas à accéder à la BDD.


    MainActivity.java :

    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
     
    public class MainActivity extends AppCompatActivity {
     
        private EditText nameDoc;
        private EditText emailDoc;
        private EditText namePa;
        private EditText ID;
     
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            nameDoc = (EditText) findViewById(R.id.editTextNameDoc);
            namePa = (EditText) findViewById(R.id.editTextNamePa);
            ID = (EditText) findViewById(R.id.editTextID);
            emailDoc = (EditText) findViewById(R.id.editTextEmail);
     
            Button btnAdd = (Button) findViewById(R.id.buttonAdd);
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addPatient();
                }
            });
        }
     
        private void addPatient()
        {
            PatientBDD patientBDD = new PatientBDD(this);
            patientBDD.openForWrite();
     
            Patient pa = new Patient(ID.getText().toString(), namePa.getText().toString(), nameDoc.getText().toString(),
                    emailDoc.getText().toString());
     
            patientBDD.insertPatient(pa);
     
            ArrayList<Patient> arr = patientBDD.getAllPatients();
            patientBDD.close();
     
            int i=0;
            for(Patient p : arr)
            {
                Log.d("PATIENT" , "********" + p.toString());
                i++;
            }
            Toast.makeText(getApplication(), "Patient add => " + i + "PATIENTS",Toast.LENGTH_LONG).show();
        }
    }
    Patient.java :

    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
     
    public class Patient
    {
        private int id;
        private String id_patient;
        private String name;
        private String doctorName;
        private String doctorEmail;
        private String pathDocMorning;
        private String pathDocAfternoon;
        private String pathDocEvening;
     
        public Patient()
        {
            pathDocMorning = "none";
            pathDocAfternoon = "none";
            pathDocEvening = "none";
        }
     
        public Patient(String id, String patName, String docName, String email)
        {
            pathDocMorning = "none";
            pathDocAfternoon = "none";
            pathDocEvening = "none";
     
            id_patient = id;
            name = patName;
     
            doctorName = docName;
            doctorEmail = email;
        }
     
     
        public void setPathDocMorning(String path) { pathDocMorning = path; }
        public void setPathDocAfternoon(String path) { pathDocAfternoon = path; }
        public void setPathDocEvening(String path) { pathDocEvening = path; }
     
        public void setID(int i) { id = i; }
        public void setNamePa(String n){ name = n; }
        public void setDoctorName(String dn) { doctorName = dn ;}
        public void setDoctorEmail(String e) { doctorEmail = e; }
        public void setId_patient(String i){ id_patient = i; }
     
        public String getNamePa() { return name; }
        public String getNameDoc() { return doctorName; }
        public String getEmail() { return doctorEmail; }
        public String getIdPatient() { return id_patient; }
        public String getPathDocMorning() { return pathDocMorning; }
        public String getPathDocAfternoon() { return  pathDocAfternoon; }
        public String getPathDocEvening() { return  pathDocEvening; }
        public int getIdBdd() { return id; }
     
        @Override
        public String toString()
        {
            StringBuilder sb = new StringBuilder();
     
            sb.append("Patient n°" + id +"\nId: " + id_patient + "\nName: " + name +
                        "\nDoctor: " + doctorName + "\nEmail: "+ doctorEmail);
     
            return sb.toString();
        }
    }
    PatientBaseSQLite.java :

    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
     
    public class PatientBaseSQLite extends SQLiteOpenHelper
    {
        private static final String TABLE_PATIENT = "table_patient";
        private static final String COL_ID = "ID";
        private static final String COL_ID_PATIENT = "ID_PATIENT";
        private static final String COL_NAME_PATIENT = "NAME_PATIENT";
        private static final String COL_NAME_DOC = "NAME_DOCTOR";
        private static final String COL_EMAIL_DOC = "EMAIL_DOCTOR";
        private static final String COL_PATH_MORNING = "PATH_MORNING";
        private static final String COL_PATH_AFTERNOON = "PATH_AFTERNOON";
        private static final String COL_PATH_EVENING = "PATH_EVENING";
     
        private static final String CREATE_BDD = " CREATE TABLE "+TABLE_PATIENT+" ("+COL_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COL_ID_PATIENT+" TEXT NOT NULL, "
                +COL_NAME_PATIENT+ " TEXT NOT NULL, "
                + COL_NAME_DOC+ " TEXT NOT NULL, "
                + COL_EMAIL_DOC+ " TEXT NOT NULL, "
                +COL_PATH_MORNING + " TEXT NOT NULL, "
                +COL_PATH_AFTERNOON+ " TEXT NOT NULL, "
                +COL_PATH_EVENING+ " TEXT NOT NULL);";
     
        public PatientBaseSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
        {
            super(context, name, factory, version);
        }
     
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(CREATE_BDD);
        }
     
        @Override
        public void onUpgrade(SQLiteDatabase db, int i, int i1)
        {
            db.execSQL("DROP TABLE "+ TABLE_PATIENT);
            onCreate(db);
        }
    }
    PatientBDD.java :

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
     
    public class PatientBDD
    {
        private static final int VERSION = 1;
        private static final String NAME_BDD = "patient.db";
        private static final String TABLE_PATIENT = "table_patient";
     
        private static final String COL_ID = "ID";
        private static final int NUM_COL_ID = 0;
        private static final String COL_ID_PATIENT = "ID_PATIENT";
        private static final int NUM_COL_ID_PATIENT = 1;
        private static final String COL_NAME_PATIENT = "NAME_PATIENT";
        private static final int NUM_COL_NAME_PATIENT = 2;
        private static final String COL_NAME_DOC = "NAME_DOCTOR";
        private static final int NUM_COL_NAME_DOC = 3;
        private static final String COL_EMAIL_DOC = "EMAIL_DOCTOR";
        private static final int NUM_COL_EMAIL_DOC = 4;
        private static final String COL_PATH_MORNING = "PATH_MORNING";
        private static final int NUM_COL_PATH_MORNING = 5;
        private static final String COL_PATH_AFTERNOON = "PATH_AFTERNOON";
        private static final int NUM_COL_PATH_AFTERNOON = 6;
        private static final String COL_PATH_EVENING = "PATH_EVENING";
        private static final int NUM_COL_PATH_EVENING = 7;
     
        private SQLiteDatabase bdd;
        private PatientBaseSQLite patients;
     
        public PatientBDD(Context context)
        {
            patients = new PatientBaseSQLite(context, NAME_BDD, null, VERSION);
        }
     
        public void openForWrite()
        {
            bdd = patients.getWritableDatabase();
        }
     
        public void openForRead()
        {
            bdd = patients.getReadableDatabase();
        }
     
        public void close()
        {
            bdd.close();
        }
     
        public SQLiteDatabase getBDD()
        {
            return bdd;
        }
     
        public long insertPatient(Patient patient)
        {
            ContentValues content = new ContentValues();
     
            content.put(COL_ID_PATIENT, patient.getIdPatient());
            content.put(COL_NAME_PATIENT, patient.getNamePa());
            content.put(COL_NAME_DOC, patient.getNameDoc());
            content.put(COL_EMAIL_DOC, patient.getEmail());
            content.put(COL_PATH_MORNING, patient.getPathDocMorning());
            content.put(COL_PATH_AFTERNOON, patient.getPathDocAfternoon());
            content.put(COL_PATH_EVENING, patient.getPathDocEvening());
            Log.e("INSERTION", "INSERT : OK");
            return bdd.insert(TABLE_PATIENT, null, content);
        }
     
        public int updatePatient(int id, Patient patient)
        {
            ContentValues content = new ContentValues();
     
            content.put(COL_ID_PATIENT, patient.getIdPatient());
            content.put(COL_NAME_PATIENT, patient.getNamePa());
            content.put(COL_NAME_DOC, patient.getNameDoc());
            content.put(COL_EMAIL_DOC, patient.getEmail());
            content.put(COL_PATH_MORNING, patient.getPathDocMorning());
            content.put(COL_PATH_AFTERNOON, patient.getPathDocAfternoon());
            content.put(COL_PATH_EVENING, patient.getPathDocEvening());
     
            return bdd.update(TABLE_PATIENT, content, COL_ID + " = " + id, null);
        }
     
        public int removePatientByID(String id_pa)
        {
            return bdd.delete(TABLE_PATIENT, COL_ID_PATIENT + " = " + id_pa, null);
        }
     
        public int removePatientByName(String name)
        {
            return bdd.delete(TABLE_PATIENT, COL_NAME_PATIENT + " = " + name, null);
        }
     
        public Patient getPatientByName(String name)
        {
            Cursor c = bdd.query(TABLE_PATIENT,
                                new String[]{ COL_ID, COL_ID_PATIENT, COL_NAME_PATIENT, COL_NAME_DOC, COL_EMAIL_DOC, COL_PATH_MORNING
                                                    , COL_PATH_AFTERNOON, COL_PATH_EVENING},
                                COL_NAME_PATIENT + " LIKE \"" + name + "\"", null, null, null, COL_NAME_PATIENT);
     
            return cursorToPatient(c);
        }
     
        public Patient cursorToPatient(Cursor c)
        {
            if(c.getCount() == 0)
            {
                c.close();
                return null;
            }
            Patient patient = new Patient();
     
            patient.setID(c.getInt(NUM_COL_ID));
            patient.setId_patient(c.getString(NUM_COL_ID_PATIENT));
            patient.setNamePa(c.getString(NUM_COL_NAME_PATIENT));
            patient.setDoctorName(c.getString(NUM_COL_NAME_DOC));
            patient.setDoctorEmail(c.getString(NUM_COL_EMAIL_DOC));
            patient.setPathDocMorning(c.getString(NUM_COL_PATH_MORNING));
            patient.setPathDocAfternoon(c.getString(NUM_COL_PATH_AFTERNOON));
            patient.setPathDocEvening(c.getString(NUM_COL_PATH_EVENING));
     
            c.close();
            return patient;
        }
     
        public ArrayList<Patient> getAllPatients()
        {
            Cursor c = bdd.query(TABLE_PATIENT,
                    new String[]{ COL_ID, COL_ID_PATIENT, COL_NAME_PATIENT, COL_NAME_DOC, COL_EMAIL_DOC, COL_PATH_MORNING
                            , COL_PATH_AFTERNOON, COL_PATH_EVENING},
                    null, null, null, null, COL_NAME_PATIENT);
     
            if(c.getCount() == 0)
            {
                c.close();
                return null;
            }
     
            ArrayList<Patient> patientList = new ArrayList<>();
     
            while(c.moveToNext())
            {
                Patient patient = new Patient();
     
                patient.setID(c.getInt(NUM_COL_ID));
                patient.setId_patient(c.getString(NUM_COL_ID_PATIENT));
                patient.setNamePa(c.getString(NUM_COL_NAME_PATIENT));
                patient.setDoctorName(c.getString(NUM_COL_NAME_DOC));
                patient.setDoctorEmail(c.getString(NUM_COL_EMAIL_DOC));
                patient.setPathDocMorning(c.getString(NUM_COL_PATH_MORNING));
                patient.setPathDocAfternoon(c.getString(NUM_COL_PATH_AFTERNOON));
                patient.setPathDocEvening(c.getString(NUM_COL_PATH_EVENING));
            }
            c.close();
            return patientList;
        }
    }
    Si quelqu'un a une piste de recherche je suis preneuse

    Cordialement

  2. #2
    Modérateur
    Avatar de grunk
    Homme Profil pro
    Lead dév - Architecte
    Inscrit en
    Août 2003
    Messages
    6 693
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Lead dév - Architecte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2003
    Messages : 6 693
    Par défaut
    Si les données ne s'insère pas c'est qu'il y'a une erreur. Si erreur il y'a elle sera forcément affichée dans le logcat.
    Pry Framework php5 | N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mai 2017
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2017
    Messages : 12
    Par défaut
    Bonjour,

    Je ne vois aucune erreur dans le logcat. Ci-joint la photo.
    Chaque message rouge indique que j'ai cliqué sur ajouter.

    Nom : logcat.png
Affichages : 112
Taille : 272,6 Ko

  4. #4
    Modérateur
    Avatar de grunk
    Homme Profil pro
    Lead dév - Architecte
    Inscrit en
    Août 2003
    Messages
    6 693
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Lead dév - Architecte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2003
    Messages : 6 693
    Par défaut
    Tu log "INSERT OK" avant même d'appeler insert

    Commence par débugger en testant le retour d'insert :

    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
    public long insertPatient(Patient patient)
    {
        ContentValues content = new ContentValues();
     
        content.put(COL_ID_PATIENT, patient.getIdPatient());
        content.put(COL_NAME_PATIENT, patient.getNamePa());
        content.put(COL_NAME_DOC, patient.getNameDoc());
        content.put(COL_EMAIL_DOC, patient.getEmail());
        content.put(COL_PATH_MORNING, patient.getPathDocMorning());
        content.put(COL_PATH_AFTERNOON, patient.getPathDocAfternoon());
        content.put(COL_PATH_EVENING, patient.getPathDocEvening());
     
        long res = bdd.insert(TABLE_PATIENT, null, content);
        if(res > 0) {
            Log.e("INSERTION", "INSERT : OK");
        } else {
            Log.e("INSERTION", "INSERT : KO , return = "+res);
        }
       return res;
    }
    Pry Framework php5 | N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réparation base de données SQlite
    Par jacquesdx dans le forum Django
    Réponses: 4
    Dernier message: 24/01/2009, 13h28
  2. Ouverture base de données SQLite avec QT
    Par MlleMR dans le forum Bases de données
    Réponses: 3
    Dernier message: 20/12/2008, 19h53
  3. Réponses: 2
    Dernier message: 05/12/2008, 10h22
  4. Classe de gestion de base de données SQLITE
    Par Munkey74 dans le forum Contribuez / Téléchargez Sources et Outils
    Réponses: 1
    Dernier message: 09/08/2007, 16h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo