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ée SQLite troublante


Sujet :

Android

  1. #1
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Base de donnée SQLite troublante
    Bonjour,
    Voila j'ai une base de donnée programmée comme ceci :

    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
     
    package com.panameInc.statstt;
     
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.Toast;
     
    public class db {
     
    	public static final String KEY_ID = "ID";
    	public static final String KEY_NAME = "Nom";
    	public static final String KEY_VALUE = "Valeur";
     
    	private static final String DATABASE_NAME = "Db_Stat";
    	private static final String DATABASE_TABLE = "table_point";
    	private static final int DATABASE_VERSION = 1;
     
     
    	private DbHelper ourHelper;
    	private final Context ourContext;
    	private SQLiteDatabase ourDatabase;
     
    		private static class DbHelper extends SQLiteOpenHelper{
     
    			public DbHelper(Context context) {
    				super(context, DATABASE_NAME, null, DATABASE_VERSION);
    				// TODO Auto-generated constructor stub
    			}
     
    			@Override
    			public void onCreate(SQLiteDatabase db) {
    				// TODO Auto-generated method stub
     
    				db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +
    						KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    						KEY_NAME + " TEXT NOT NULL, " +
    						KEY_VALUE + " INTEGER);"
    						);
    			}
     
    			@Override
    			public void onUpgrade(SQLiteDatabase db, int oldVersion,
    					int newVersion) {
    				// TODO Auto-generated method stub
    				db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    				onCreate(db);
    			}
     
    		}
     
    		public db(Context c){
    			ourContext = c;
     
    		}
     
    		public db open() throws SQLException{
    			ourHelper = new DbHelper(ourContext);
    			ourDatabase = ourHelper.getWritableDatabase();
    			return this;
    		}
     
    		public void close(){
    			ourHelper.close();
    		}
     
    		public long insert(Point p){
     
    			ContentValues cv = new ContentValues();
    			cv.put(KEY_NAME, p.getNom());
    			cv.put(KEY_VALUE, p.getValeur());
    			return ourDatabase.insert(db.DATABASE_TABLE, null, cv);
    		}
     
    		public void update(int ID, String nom, int valeur) throws SQLException, NullPointerException{
    			ContentValues cv = new ContentValues();
    			cv.put(KEY_NAME, nom);
    			cv.put(KEY_VALUE, valeur);
    			ourDatabase.update(db.DATABASE_TABLE, cv, KEY_ID +  " = " +ID, null);
    		}
     
    		public void delete(int id) throws SQLException, NullPointerException{
    			ourDatabase.delete(db.DATABASE_TABLE, KEY_ID +" = " +id, null);
    		}
     
    		public void deleteAll() throws SQLException , NullPointerException{
    			int id = 0;
    			ourDatabase = ourHelper.getWritableDatabase();
    			for(id = 0; id <= 55; id++)
    				ourDatabase.delete(db.DATABASE_TABLE, KEY_ID +" = " +id, null);
    		}
    		public Point getData(String nom){
     
    			String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_VALUE};
    			Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + " LIKE \"" + nom +"\"", null, null, null, null);
     
    			int iRow = c.getColumnIndex(KEY_ID);
    			int iName = c.getColumnIndex(KEY_NAME);
    			int iValue = c.getColumnIndex(KEY_VALUE);
     
    			if (c.getCount() == 0)
    				return null;
     
    			//Sinon on se place sur le premier élément
    			c.moveToFirst();
    			//On créé un livre
    			Point p = new Point();
    			//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
    			p.setId(c.getInt(iRow));
    			p.setNom(c.getString(iName));
    			p.setValeur(c.getInt(iValue));
    			//On ferme le cursor
    			c.close();
     
    			//On retourne le livre
    			return p;
     
    		}
     
    }
    et cette base de donnée ne me fais obtenir que la moitié de mes requètes, et j'obtiens parfois des NullPointerExceptions sur le getData().


    Voici la classe Point si cela peut vous aider :
    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
     
    package com.panameInc.statstt;
     
    public class Point {
     
    	private int Id = 0;
    	private String nom = null;
    	private int valeur = 0;
     
     
    	public Point(){
    	}
     
    	public Point(String _nom, int _valeur, int _id){
    		this.nom = _nom;
    		this.valeur = _valeur;
    		this.Id = _id;
    	}
     
    	public Point(String _nom, int _valeur){
    		this.nom = _nom;
    		this.valeur = _valeur;
    	}
     
    //--------------------------- Accesseurs -----------------------------------------------------
     
    	public String getNom(){
    		return nom;
    	}
     
    	public int getValeur(){
    		return valeur;
    	}
     
    	public int getId(){
    		return Id;
    	}
     
    //--------------------------- Mutateurs -------------------------------------------------------
     
    	public void setNom(String _nom){
    		this.nom = _nom;
    	}
     
    	public void setValeur(int _valeur){
    		this.valeur = _valeur;
    	}
     
    	public void setId(int _Id){
    		this.Id = _Id;
    	}
     
    }
    Merci d'avance !

  2. #2
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    if (c.getCount() == 0)
    	return null;

    S'il n'y a pas de livre, tu retournes null. Est-ce de là que viennent tes NPE ?

    Si ce n'est pas le cas, merci de nous fournir le LogCat d'une exception, ainsi que la manière dont tu te sers de ta BDD.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  3. #3
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    En fait j'ai un fragment :
    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
     
    package com.panameInc.statstt;
     
    import com.panameInc.statstt.R;
     
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class Set1Fragment extends Fragment {
     
    	private TextView CGCDVA = null;
    	private TextView CGRVVA = null;
    	private TextView CGSEVA = null;
    	private TextView FNCDVA = null;
    	private TextView FNRVVA = null;
    	private TextView FNSEVA = null;
    	private TextView FDCDVA = null;
    	private TextView FDRVVA = null;
    	private TextView FDSEVA = null;
    	private TextView CGCDP = null;
    	private TextView CGRVP = null;
    	private TextView CGSEP = null;
    	private TextView FNCDP = null;
    	private TextView FNRVP = null;
    	private TextView FNSEP = null;
    	private TextView FDCDP = null;
    	private TextView FDRVP = null;
    	private TextView FDSEP = null;
     
     
     
    	private Point a = null;
    	private Point b = null;
    	private Point c = null;
    	private Point d = null;
    	private Point e = null;
    	private Point f = null;
    	private Point g = null;
    	private Point h = null;
    	private Point i = null;
    	private Point j = null;
    	private Point k = null;
     
    	int total = 0;
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
     
            View rootView = inflater.inflate(R.layout.fragment_set1, container, false);
     
        	db base= new db(this.getActivity());
     
            base.open();
     
            j = base.getData("tp21");
            k = base.getData("tp11");
     
    	    Toast.makeText(getActivity(), "tP1_1 = " +j.getValeur(), Toast.LENGTH_SHORT).show();
     
            total = j.getValeur() + k.getValeur();
     
            a.setValeur(base.getData("cgcd1").getValeur());
            b.setValeur(base.getData("cgrv1").getValeur());
            c.setValeur(base.getData("cgse1").getValeur());
            d.setValeur(base.getData("fncd1").getValeur());
            e.setValeur(base.getData("fnrv1").getValeur());
            f.setValeur(base.getData("fnse1").getValeur());
            g.setValeur(base.getData("fdcd1").getValeur());
            h.setValeur(base.getData("fdrv1").getValeur());
            i.setValeur(base.getData("fdse1").getValeur());
     
            CGCDVA = (TextView) rootView.findViewById(R.id.CGCDVA1);
            CGCDP = (TextView) rootView.findViewById(R.id.CGCDP1);
            CGRVVA = (TextView) rootView.findViewById(R.id.CGRVVA1);
            CGRVP = (TextView) rootView.findViewById(R.id.CGRVP1);
            CGSEVA = (TextView) rootView.findViewById(R.id.CGSEVA1);
            CGSEP = (TextView) rootView.findViewById(R.id.CGSEP1);
     
            FNCDVA = (TextView) rootView.findViewById(R.id.FNCDVA1);
            FNCDP = (TextView) rootView.findViewById(R.id.FNCDP1);
            FNRVVA = (TextView) rootView.findViewById(R.id.FNRVVA1);
            FNRVP = (TextView) rootView.findViewById(R.id.FNRVP1);
            FNSEVA = (TextView) rootView.findViewById(R.id.FNSEVA1);
            FNSEP = (TextView) rootView.findViewById(R.id.FNSEP1);
     
            FDCDVA = (TextView) rootView.findViewById(R.id.FDCDVA1);
            FDCDP = (TextView) rootView.findViewById(R.id.FDCDP1);
            FDRVVA = (TextView) rootView.findViewById(R.id.FDRVVA1);
            FDRVP = (TextView) rootView.findViewById(R.id.FDRVP1);
            FDSEVA = (TextView) rootView.findViewById(R.id.FDSEVA1);
            FDSEP = (TextView) rootView.findViewById(R.id.FDSEP1);
     
            CGCDVA.setText(Integer.toString(a.getValeur()));
            CGRVVA.setText(Integer.toString(b.getValeur()));
            CGSEVA.setText(Integer.toString(c.getValeur()));
            FNCDVA.setText(Integer.toString(d.getValeur()));
            FNRVVA.setText(Integer.toString(e.getValeur()));
            FNSEVA.setText(Integer.toString(f.getValeur()));
            FDCDVA.setText(Integer.toString(g.getValeur()));
            FDRVVA.setText(Integer.toString(h.getValeur()));
            FDSEVA.setText(Integer.toString(i.getValeur()));
     
            CGCDP.setText(Double.toString(Calculs.pourcent(a.getValeur(), total)) + " %");
            CGRVP.setText(Double.toString(Calculs.pourcent(b.getValeur(), total)) + " %");
            CGSEP.setText(Double.toString(Calculs.pourcent(c.getValeur(), total)) + " %");
            FNCDP.setText(Double.toString(Calculs.pourcent(d.getValeur(), total)) + " %");
            FNRVP.setText(Double.toString(Calculs.pourcent(e.getValeur(), total)) + " %");
            FNSEP.setText(Double.toString(Calculs.pourcent(f.getValeur(), total)) + " %");
            FDCDP.setText(Double.toString(Calculs.pourcent(g.getValeur(), total)) + " %");
            FDRVP.setText(Double.toString(Calculs.pourcent(h.getValeur(), total)) + " %");
            FDSEP.setText(Double.toString(Calculs.pourcent(i.getValeur(), total)) + " %");
     
     
            return rootView;
        }
     
    }
    Voila .. Au début je pensais que c'étais le context qui n'allais pas ....
    Images attachées Images attachées  

  4. #4
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    A priori, c'est cette ligne qui déconne total = j.getValeur() + k.getValeur();.
    Ligne 69 de la classe Set1Fragment (qui n'est pas un bon nom de classe... aucune indication sur ce que c'est sensé être... mais vu que les variables sont toutes nommées sans réelle indication...).

    Au vu du code, tu n'aurais pas de "livre"/Point (concordance...) correspondant à "tp21" ou à "tp11".
    Au vu de ce que tu sembles essayer de faire, je te suggère très fortement de t'intéresser aux Adapter et aux ListView.

    Ps : en Java, seule les constantes (final static) doivent être en majuscule. Classes en UpperCamelCase, variables & méthodes en lowerCamelCase.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  5. #5
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Voici l'endroit où j'ajoute la ligne "tp11" et "tp21"
    La classe se nomme Statistiques1

    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
     
    Point a = new Point("tp11", tP1_1);
    				    Point b = new Point("tp21", tP2_1);
    				    Point c = new Point("cgse1", CgSe_1);
    				    Point d = new Point("cgcd1", CgCd_1);
    				    Point e = new Point("cgrv1", CgRv_1);
    				    Point f = new Point("fnse1", FnSe_1);
    				    Point g = new Point("fncd1", FnCd_1);
    				    Point h = new Point("fnrv1", FnRv_1);
    				    Point i = new Point("fdse1", FdSe_1);
    				    Point j = new Point("fdcd1", FdCd_1);
    				    Point k = new Point("fdrv1", FdRv_1);
     
    				    base.open();
    				    base.insert(a);
    				    base.insert(b);
    				    base.insert(c);
    				    base.insert(d);
    				    base.insert(e);
    				    base.insert(f);
    				    base.insert(g);
    				    base.insert(h);
    				    base.insert(i);
    				    base.insert(j);
    				    base.insert(k);
     
    				    Toast.makeText(Statistiques1.this, "tp1_1 = "+base.getData("tp11").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "tp2_1 = "+base.getData("tp21").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "cgse1 = "+base.getData("cgse1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "cgcd1 = "+base.getData("cgcd1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "cgrv1 = "+base.getData("cgrv1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fnse1 = "+base.getData("fnse1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fncd1 = "+base.getData("fncd1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fnrv1 = "+base.getData("fnrv1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fdse1 = "+base.getData("fdse1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fdcd1 = "+base.getData("fdcd1").getValeur(), Toast.LENGTH_SHORT).show();
    				    Toast.makeText(Statistiques1.this, "fdrv1 = "+base.getData("fdrv1").getValeur(), Toast.LENGTH_SHORT).show();
     
    				    base.close();
    Et sinon je serais interressé par les adapters ou listes mais je ne sais pas utiliser ce genre de choses

  6. #6
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Tu n'as toujours pas fourni l'erreur : exception + stacktrace complet + code incriminé.

    Pour les ListView, tu as une pléthore de tutoriels. En voici trois :
    http://mickael-lt.developpez.com/tut...iser-listview/
    http://a-renouard.developpez.com/tut...iser-listview/
    https://developer.android.com/guide/.../listview.html
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  7. #7
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    C'est bon j'ai compris mon erreur. Merci a toi !!

+ 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