SQLiteException 没有这样的 table ITEMS (code1):编译时:SELECT * FROM ITEMS WHERE RECID =?

SQLiteException no such table ITEMS (code1) : while compiling: SELECT * FROM ITEMS WHERE RECID =?

我正在尝试创建一个 table ITEMS,其中 FOREIGN KEY 引用另一个 table UTILISATEURS 的 ID... 而我的table UTILISATEURS 工作正常,table ITEMS 由于某种原因没有创建。错误似乎在以下查询中:SELECT * FROM ITEMS WHERE RECID = ?.

这是我的classItemsDataBaseAdapter

 public class ItemsDataBaseAdapter {

    static final String DATABASE_NAME = "Panier.db";
    static final int DATABASE_VERSION = 2;
    public static final int NAME_COLUMN = 1;
    // TODO: Créer des attributs publics pour chaque colonne dans la BD
    // Requête SQL qui crée la DataBase

    static final String DATABASE_CREATE = "create table "+"ITEMS"+
            "( " + "NAME text,PRICE integer,STORE text,IMAGEPATH text,RECID integer,ITEMID integer primary key autoincrement,"+ " FOREIGN KEY (RECID) REFERENCES UTILISATEURS (RECID));";

    public  SQLiteDatabase db;
    /*** Contexte de l'application qui utilise la DataBase***/

private final Context context;

    private ItemsDataBaseHelper dbHelper;
    public  ItemsDataBaseAdapter(Context _context) 
    {
        context = _context;
        dbHelper = new ItemsDataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    public  ItemsDataBaseAdapter open() throws SQLException 
    {
        db = dbHelper.getWritableDatabase();
        return this;
    }
    public void close() 
    {
        db.close();
    }

    public  SQLiteDatabase getDatabaseInstance()
    {
        return db;
    }

    public void insererEntree(String nomItem, Integer prix, String epicerie, String imagePath)
    {
       ContentValues values = new ContentValues();
        values.put("NAME", nomItem);
        values.put("PRICE", prix);
        values.put("STORE", epicerie);
        values.put("IMAGEPATH", imagePath);


        db.insert("ITEMS", null, values);
    }
    public int supprimeEntree(String nom)
    {
        String where="NAME=?";
        int nbrEntreeSupp= db.delete("ITEMS", where, new String[]{nom});
        return nbrEntreeSupp;
    }   
    /*public ArrayList<Item> getEntreesUtilisateur(String id)
    {
        Cursor cursor=db.query("ITEMS", null, " RECID=?", new String[]{id}, null, null, null);
        if(cursor.getCount()<1) // Le RecID n<existe pas
        {
            cursor.close();
            return null;
        }
        cursor.moveToFirst();
        ArrayList<Item> listItems= cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return mdp;             
    }*/

    /***Va chercher tous les items associés à l'utilisateur***/

    public ArrayList<Item> getEntreesUtilisateur(String id) 
    {
        ArrayList<Item> listeItem = new ArrayList<Item>();
        Item itemCourant;
        Cursor c = db.rawQuery("SELECT * FROM ITEMS WHERE RECID = ?", new String [] {id}); 
        while (c.moveToNext())
        {
            String name = c.getString(c.getColumnIndex("NAME"));
            int prix = c.getInt(c.getColumnIndex("PRICE"));
            String epicerie = c.getString(c.getColumnIndex("STORE"));
            String imagePath = c.getString(c.getColumnIndex("IMAGEPATH"));

            try
            {
                itemCourant = new Item();
                itemCourant.setNom(name);
                itemCourant.setPrix(prix);
                itemCourant.setEndroit(epicerie);
                itemCourant.setImagePath(imagePath);
                listeItem.add(itemCourant);
            }
            catch (Exception e) {
                Log.e("Ca a pas marché :(", "Error " + e.toString());
            }

        }

        c.close();

        db.close();
        return listeItem;
    }
    public void  updateEntree(String nom,int prix, String epicerie, String imagePath)
    {
        // Définition des colonnes modifiées
        ContentValues updatedValues = new ContentValues();
        // Allouer des valeurs pour chaque colonne
        updatedValues.put("NAME", nom);
        updatedValues.put("PRICE",prix);
        updatedValues.put("STORE",epicerie);
        updatedValues.put("IMAGEPATH",imagePath);

        String where="NAME = ?";
        db.update("ITEMS",updatedValues, where, new String[]{nom});            
    }

    }

这是我的classItemsDataBaseHelper

 public class ItemsDataBaseHelper extends SQLiteOpenHelper
    {

    public ItemsDataBaseHelper(Context context, String nom,CursorFactory factory, int version) 
    {
               super(context, nom, factory, version);
    }

    /*** Appelé s'il n'y a pas de BD pour en créer une***/

    @Override
    public void onCreate(SQLiteDatabase _db) 
    {
            _db.execSQL(ItemsDataBaseAdapter.DATABASE_CREATE);

    }
    /*** Appelé s'il y a déjà une BD et modifie les données dans celle-ci**/

    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
    {

            // Upgrade the existing database to conform to the new version. Multiple
            // previous versions can be handled by comparing _oldVersion and _newVersion
            // values.
            // The simplest case is to drop the old table and create a new one.
            _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
            //Créer une nouvelle BD
            onCreate(_db);
    }


    }

这是我的classUtilisateursDataBaseAdapter

public class UtilisateursDataBaseAdapter
 {
    static final String DATABASE_NAME = "Panier.db";
    static final int DATABASE_VERSION = 2;
    public static final int NAME_COLUMN = 1;
    // TODO: Créer des attributs publics pour chaque colonne dans la BD
    /*** Requête SQL qui crée la DataBase***/

    static final String DATABASE_CREATE = "create table "+"UTILISATEURS"+
            "( " + "USERNAME  text,PASSWORD text,RECID"+ " integer primary key autoincrement);";

    public  SQLiteDatabase db;
    /*** Contexte de l'application qui utilise la DataBase***/

    private final Context context;

    private UtilisateursDataBaseHelper dbHelper;
    public  UtilisateursDataBaseAdapter(Context _context) 
    {
        context = _context;
        dbHelper = new UtilisateursDataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public  UtilisateursDataBaseAdapter open() throws SQLException 
    {
        db = dbHelper.getWritableDatabase();
        return this;
    }
    public void close() 
    {
        db.close();
    }

    public  SQLiteDatabase getDatabaseInstance()
    {
        return db;
    }

    public void insererEntree(String identifiant,String mdp)
    {
       ContentValues values = new ContentValues();
        values.put("USERNAME", identifiant);
        values.put("PASSWORD",mdp);


        db.insert("UTILISATEURS", null, values);
    }
    public int supprimeEntree(String identifiant)
    {
        String where="USERNAME=?";
        int nbrEntreeSupp= db.delete("UTILISATEURS", where, new String[]{identifiant}) ;
        return nbrEntreeSupp;
    }   
    public String getEntreeMdp(String identifiant)
    {
        Cursor cursor=db.query("UTILISATEURS", null, " USERNAME=?", new String[]{identifiant}, null, null, null);
        if(cursor.getCount()<1) // l'identifiant n'exite pas
        {
            cursor.close();
            return "NOT EXIST";
        }
        cursor.moveToFirst();
        String mdp= cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return mdp;             
    }
    public void  updateEntree(String identifiant,String mdp)
    {
        // Définition des colonnes modifiées
        ContentValues updatedValues = new ContentValues();
        // Allouer des valeurs pour chaque colonne
        updatedValues.put("USERNAME", identifiant);
        updatedValues.put("PASSWORD",mdp);

        String where="USERNAME = ?";
        db.update("UTILISATEURS",updatedValues, where, new String[]{identifiant});             
    }

    public int getRecIdUtilisateur(String identifiant)
    {
        Cursor cursor=db.query("UTILISATEURS", null, " USERNAME=?", new String[]{identifiant}, null, null, null);
        if(cursor.getCount()<1) // l'identifiant n'exite pas
        {
            cursor.close();
            return -1;
        }
        cursor.moveToFirst();
        int recId = cursor.getInt(cursor.getColumnIndex("RECID"));
        cursor.close();
        return recId;
    }




}

最后,这是我的classUtilisateursDataBaseHelper

public class UtilisateursDataBaseHelper extends SQLiteOpenHelper
{
public UtilisateursDataBaseHelper(Context context, String nom,CursorFactory factory, int version) 
{
           super(context, nom, factory, version);
}
// Appelé s'il n'y a pas de BD pour en créer une
@Override
public void onCreate(SQLiteDatabase _db) 
{
        _db.execSQL(UtilisateursDataBaseAdapter.DATABASE_CREATE);

}
// Appelé s'il y a déjà une BD et modifie les données dans celle-ci
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
{

        // Upgrade the existing database to conform to the new version. Multiple
        // previous versions can be handled by comparing _oldVersion and _newVersion
        // values.
        // The simplest case is to drop the old table and create a new one.
        _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
        //Créer une nouvelle BD
        onCreate(_db);
}


}

我终于解决了我的问题。如果您在同一个数据库中创建多个 table,最好将它们包装在同一个 DbHelper 中,因此在同一个 DbHelper 实例中创建它们。