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

SQLite Discussion :

Requête imbriquée en SQLite


Sujet :

SQLite

  1. #1
    Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2014
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Requête imbriquée en SQLite
    Bonjour,
    Je suis étudiante et travaille sur une application android avec une base de données SQLite
    Voici le schéma relationnel :
    Ingredient[idingr,libelle]
    Recette[idrec,titre]
    Necessiter[#idrec,#idingr,quantite]

    Je suis habituée au MySQL ou pgSQL et j'ai fait une requête et je voudrais savoir si l'un d'entre vous pourrait m'aider à la transformer pour SQLite :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Select distinct r.titre 
    from recette r 
    where r.idrec not in 
     (select distinct r.idrec 
      from recette r 
       join Necessiter n on r.idrec=n.idrec 
       join Ingredient i on i.idingr=n.idingr 
      where i.idingr in (1,2,3);
    Merci de votre aide

  2. #2
    Membre éprouvé
    Homme Profil pro
    Chef de projets retraité
    Inscrit en
    Juillet 2011
    Messages
    420
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : Chef de projets retraité
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2011
    Messages : 420
    Points : 1 102
    Points
    1 102
    Par défaut
    Bonjour,

    Je n'ai pas les schémas des tables et des données pour vérifier mais les sous requêtes fonctionnent dans SQLITE.


    Par contre dans ton SQL il te manque une parenthèse pour fermer la sous requête (à mettre avant le point virgule)

    Cordialement

  3. #3
    Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2014
    Messages : 3
    Points : 2
    Points
    2
    Par défaut le code
    Bonjour,

    J'ai essayé d'implémenter ma solution mais j'ai cette erreur :
    sqllite.SQLiteException: no such column id(code1) : , while compiling : SELECT idC, nom, description FROM Cocktail2 WHERE id = ?

    voici le code de Ma base de données :

    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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    public class MySQLiteHelper extends SQLiteOpenHelper{
     
     
     
    	protected final static int DATABASE_VERSION = 1;
        protected final static String DATABASE_NAME= "funcocktail.db";  
     
        //----------------- TABLE COCKTAIL-------------------//
        protected final static String TABLE_COCKTAIL="Cocktail2";
        protected final static String COCKTAIL_KEY_ID="idC";
        protected final static String COCKTAIL_NOM="nom";
        protected final static String COCKTAIL_DESCRIPTION="description";    
        protected final static String[] TABLE_COCKTAIL_COLUMNS={COCKTAIL_KEY_ID,COCKTAIL_NOM,COCKTAIL_DESCRIPTION};
     
     
     
      //----------------- TABLE INGREDIENT-------------------//
        protected final static String TABLE_INGREDIENT="Ingredient2";
        protected final static String INGREDIENT_KEY_ID="idI";
        protected final static String INGREDIENT_NOM="nom"; 
        protected final static String[] TABLE_INGREDIENT_COLUMNS={INGREDIENT_KEY_ID,INGREDIENT_NOM};
     
     
        //----------------- TABLE RECETTE-------------------//
        protected final static String TABLE_RECETTE="Recette2";
        protected final static String RECETTE_KEY_ID_ING="id_ing";
        protected final static String RECETTE_KEY_COCK="id_cock"; 
        protected final static String RECETTE_QUANTIE="quantite";
        protected final static String[] TABLE_RECETTE_COLUMNS={RECETTE_KEY_ID_ING,RECETTE_KEY_COCK};    
     
     
    	public MySQLiteHelper(Context context) {
    		super(context,DATABASE_NAME,null,DATABASE_VERSION);
    		// TODO Auto-generated constructor stub
    	}
     
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		String CREATE_COCKTAIL_TABLE = "CREATE TABLE Cocktail2 ( " +
                    "idC INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "nom TEXT, "+
                    "description TEXT )";
     
    		String CREATE_INGREDIENT_TABLE = "CREATE TABLE Ingredient2 ( " +
                    "idI INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "nom TEXT )";
     
    		String CREATE_RECETTE_TABLE = "CREATE TABLE Recette2 ( " +
                    "id_ing INTEGER NOT NULL REFERENCES Ingredient2 , " + 
                    "id_cock INTEGER NOT NULL REFERENCES Cocktail2 , " +
                    "quantite INTEGER, " +
                    "PRIMARY KEY ("+RECETTE_KEY_ID_ING+","+RECETTE_KEY_COCK+"), "+
                    "FOREIGN KEY("+RECETTE_KEY_ID_ING+") REFERENCES "+TABLE_INGREDIENT+" ("+INGREDIENT_KEY_ID+"), " +
                    "FOREIGN KEY("+RECETTE_KEY_COCK+") REFERENCES "+TABLE_COCKTAIL+" ("+COCKTAIL_KEY_ID+") )";
     
            // create table
            db.execSQL(CREATE_COCKTAIL_TABLE);
            db.execSQL(CREATE_INGREDIENT_TABLE);
            db.execSQL(CREATE_RECETTE_TABLE);
     
    	}
     
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    		db.execSQL("DROP TABLE IF EXISTS Cocktail2");
    		db.execSQL("DROP TABLE IF EXISTS Ingredient2");
    		db.execSQL("DROP TABLE IF EXISTS Recette2");
     
            this.onCreate(db);
     
    	}
     
     
     
     
    		/*-------------------------  Cocktail   ------------------------   */
     
     
     
     
     
    				// ---------------CRUD------------//
     
     
    	public void AddCocktail(Cocktail c1){
            Log.d("logAddC", c1.toString());
            // 1. get reference to writable DB
            SQLiteDatabase db = this.getWritableDatabase();
     
            // 2. create ContentValues to add key "column"/value
            ContentValues values = new ContentValues();
            values.put(COCKTAIL_NOM, c1.getNomCocktail()); // get title 
            values.put(COCKTAIL_DESCRIPTION, c1.getDescription()); // get author
     
            // 3. insert
            db.insert(TABLE_COCKTAIL, // table
                    null, //nullColumnHack
                    values); // key/value -> keys = column names/ values = column values
     
            // 4. close
            db.close(); 
        }
     
    	 public Cocktail getCocktail(int id){
     
    	        // 1. get reference to readable DB
    	        SQLiteDatabase db = this.getReadableDatabase();
     
    	        // 2. build query
    	        Cursor cursor = 
    	                db.query(TABLE_COCKTAIL, // a. table
    	                TABLE_COCKTAIL_COLUMNS, // b. column names
    	                " id = ?", // c. selections 
    	                new String[] { String.valueOf(id) }, // d. selections args
    	                null, // e. group by
    	                null, // f. having
    	                null, // g. order by
    	                null); // h. limit
     
    	        // 3. if we got results get the first one
    	        if (cursor != null)
    	            cursor.moveToFirst();
     
    	        // 4. build book object
    	        Cocktail c1 = new Cocktail();
    	        c1.setID(Integer.parseInt(cursor.getString(0)));
    	        c1.setNom(cursor.getString(1));
    	        c1.setDescription(cursor.getString(2));
     
    	        Log.d("getCocktail("+id+")", c1.toString());
     
    	        // 5. return book
    	        return c1;
    	    }
     
     
    		/*-------------------------  Ingredient   ------------------------   */
     
     
     
     
     
    					// ---------------CRUD------------//	 
     
     
    	 public void addIngredient(Ingredient ing) {
     
    		 Log.e("LogAddI", ing.toString());
    	        // 1. get reference to writable DB
    	        SQLiteDatabase db = this.getWritableDatabase();
     
    	        // 2. create ContentValues to add key "column"/value
    	       // if (getIngredient(ing.getId())==null){
    	        ContentValues values = new ContentValues();
    	        values.put(INGREDIENT_NOM, ing.getNom()); // get name
     
     
    	        // 3. insert
    	        db.insert(TABLE_INGREDIENT,// table
    	                null,//nullColumnHack
    	                values); // key/value -> keys = column names/ values = column values
    	        //}
    	        // 4. close
    	        db.close();
     
    	}
     
     
    	 public Ingredient getIngredient(int id){
     
    	        // 1. get reference to readable DB
    	        SQLiteDatabase db = this.getReadableDatabase();
     
    	        // 2. build query
    	        Cursor cursor = 
    	                db.query(TABLE_INGREDIENT, // a. table
    	                TABLE_INGREDIENT_COLUMNS, // b. column names
    	                " id = ?", // c. selections 
    	                new String[] { String.valueOf(id) }, // d. selections args
    	                null, // e. group by
    	                null, // f. having
    	                null, // g. order by
    	                null); // h. limit
     
    	        // 3. if we got results get the first one
    	        if (cursor != null)
    	            cursor.moveToFirst();
     
    	        // 4. build book object
    	        Ingredient i1 = new Ingredient();
    	        i1.setID(Integer.parseInt(cursor.getString(0)));
    	        i1.setNom(cursor.getString(1));
     
    	        Log.d("getIng("+id+")", i1.toString());
     
    	        // 5. return book
    	        return i1;
    	    }
     
     
     
     
     
    	 //-------OPREATION  RECETTE-------------------------//
     
     
    	 public void AddRecette(Ingredient i,Cocktail c ,  int qte){
     
    		 // 1. get reference to writable DB
    	     SQLiteDatabase db = this.getWritableDatabase();
    	     ContentValues values = new ContentValues();
    	     values.put(RECETTE_KEY_ID_ING, i.getId()); // get ID ING
    	     values.put(RECETTE_KEY_COCK, c.getId()); // get ID ING
    	     values.put(RECETTE_QUANTIE, qte);  
     
    	        // 3. insert
    	        db.insert(TABLE_RECETTE,// table
    	                null,//nullColumnHack
    	                values); // key/value -> keys = column names/ values = column values
    	        //}
    	        // 4. close
    	        db.close();
     
    	 }
     
     
    	  // pour la selection des recettes de l'utilisateur
    	public ArrayList<Recette> getAllRecettes() {
    	        // 1. get reference to readable DB
    	        SQLiteDatabase db = this.getReadableDatabase();
     
    		Cursor c = db.query(true, TABLE_RECETTE, new String[] {
    				RECETTE_KEY_ID_ING,RECETTE_KEY_COCK,RECETTE_QUANTIE}, null, null, null, null, null,
    				null);// ok
     
    		return cursorToAllRecettes(c);
    	}
     
    	public ArrayList<Recette> cursorToAllRecettes(Cursor c) {
    		ArrayList<Recette> maListe = new ArrayList<Recette>();
    		if (c.getCount() == 0) {
    			return null;
    		}
    		c.moveToFirst();
    		for (int i = 0; i < c.getCount(); i++) {
    			Recette r = new Recette();
    			r.setID_ingredient(Integer.parseInt(c.getString(0)));
    			r.setID_cocktail(Integer.parseInt(c.getString(1)));
    			r.setQte(Integer.parseInt(c.getString(2)));
    			maListe.add(r);
    			c.moveToNext();
    		}
    		c.close();
    		return maListe;
    	}
     
     
    	////////////////////////////////////////////////////////////////////////////// LA METHODE QUI NE MARCHE PAS
     
    	public ArrayList<String> getBDCocktailCorrespond(){//recette = cocktail ing = ing necessiter recette
    		String requete ="select distinct "+
    	COCKTAIL_NOM+
    	" from "+
    	TABLE_COCKTAIL+
    	" where "+
    	COCKTAIL_KEY_ID+
    	" not in " +
    				"(select distinct " +
    				COCKTAIL_KEY_ID+
    				" from "+
    				TABLE_COCKTAIL+
    				" join "+TABLE_RECETTE+
    				" on "+COCKTAIL_KEY_ID+"="+
    				RECETTE_KEY_COCK+
    				" join "+TABLE_INGREDIENT+
    				" on "+INGREDIENT_KEY_ID
    				+"="+RECETTE_KEY_ID_ING +" where "+INGREDIENT_KEY_ID +" in (2,3)); ";
    		//GINtONIC N'A NI RHUM(2), NI MENTHE(3)
    		 // 1. get reference to readable DB
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor c = db.rawQuery(requete, null);
            return cursor(c);
    	}
     
    	public ArrayList<String> cursor(Cursor c) {
    		ArrayList<String> maListe = new ArrayList<String>();
    		if (c.getCount() == 0) {
    			return null;
    		}
    		c.moveToFirst();
    		for (int i = 0; i < c.getCount(); i++) {
    			String n =c.getString(0);
    			maListe.add(n);
    			c.moveToNext();
    		}
    		c.close();
    		return maListe;
    	}
     
     
     
     
    	 }
     
     
     
     
     
     
    Dans mon activité : 
     
    db.AddCocktail(new Cocktail("ginTonic", "description du gin"));
    db.AddCocktail(new Cocktail("mojito", "description du mojito"));
     
    db.addIngredient(new Ingredient("gin"));
    db.addIngredient(new Ingredient("menthe"));
    db.addIngredient(new Ingredient("rhum"));
     
    Cocktail c = db.getCocktail(1);//GINTONIC
     
    db.AddRecette(i1,c,2);
     
    textView2.setText(db.getBDCocktailCorrespond().get(0).toString());
    Dans le textview si ma méthode marchait le résultat aurait été gintonic
    Merci pour votre aide

  4. #4
    Membre éprouvé
    Homme Profil pro
    Chef de projets retraité
    Inscrit en
    Juillet 2011
    Messages
    420
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : Chef de projets retraité
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2011
    Messages : 420
    Points : 1 102
    Points
    1 102
    Par défaut
    Bonjour,

    Ce n'est pas du code SQL que tu postes mais un langage que je ne connais pas.
    De plus utilises les balises code!

    Donc tu as une erreur :
    sqllite.SQLiteException: no such column id(code1)
    En regardanat rapidement ton code, je vois des
    IdC/IdI/IdIng
    mais pas d'Id dans les noms de tes colonnes et c'est le sens de cette erreur!

    Cordialement

Discussions similaires

  1. Pb requête imbriquée
    Par ddams dans le forum Requêtes
    Réponses: 11
    Dernier message: 20/04/2004, 12h13
  2. [Requête] Faire une requête imbriquée?
    Par sekiryou dans le forum Requêtes
    Réponses: 2
    Dernier message: 17/01/2004, 22h52
  3. problème avec une requête imbriquée
    Par jaimepasteevy dans le forum Langage SQL
    Réponses: 13
    Dernier message: 05/12/2003, 10h29
  4. Requêtes imbriquées ?
    Par Ph. B. dans le forum XMLRAD
    Réponses: 7
    Dernier message: 06/05/2003, 13h19
  5. Requête imbriquée et indexes INTERBASE
    Par vadim dans le forum InterBase
    Réponses: 2
    Dernier message: 06/09/2002, 16h15

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