I'm trying to create a table ITEMS
with a FOREIGN KEY
that references the id of another table UTILISATEURS
... While my table UTILISATEURS
works just fine, the table ITEMS
is not created for some reason. The error seems to be in the following query : SELECT * FROM ITEMS WHERE RECID = ?
.
This is my class ItemsDataBaseAdapter
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});
}
}
This is my class ItemsDataBaseHelper
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);
}
}
This is my class UtilisateursDataBaseAdapter
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;
}
}
Finally, this is my class UtilisateursDataBaseHelper
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);
}
}
I finally resolved my problem. If you are creating more than one table in the same Database, it is better to wrap them in same DbHelper, therefore creating them in the same DbHelper instanciation.