I am creating a SqLite database in Android and have the following error :
(1) no such table: table_users
Error inserting DN=1 JOUR=1 ID=aa SEXE=0 PASSWORD=bb MOIS=1
Here is the SQL code :
public static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_USERS + " (" +
COL_ID + " TEXT PRIMARY KEY, " +
COL_PASSWORD+ " TEXT NOT NULL, " +
COL_JOUR + " TEXT NOT NULL, "+
COL_MOIS + " TEXT NOT NULL, "+
COL_DN + " TEXT NOT NULL, "+
COL_SEXE+ " INTEGER NOT NULL);";
What's wrong with this?
Here is the code where I manage the database:
public class DatabaseManager extends SQLiteOpenHelper {
private static final String TABLE_USERS = "table_users";
public static final String COL_ID ="ID";
public static final String COL_PASSWORD = "PASSWORD";
private static final String COL_JOUR = "JOUR";
private static final String COL_MOIS = "MOIS";
public static final String COL_DN = "DN";
public static final String COL_SEXE = "SEXE";
public static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_USERS + " (" +
COL_ID + " TEXT PRIMARY KEY, " +
COL_PASSWORD+ " TEXT NOT NULL, " +
COL_JOUR + " TEXT NOT NULL, "+
COL_MOIS + " TEXT NOT NULL, "+
COL_DN + " TEXT NOT NULL, "+
COL_SEXE+ " INTEGER NOT NULL);";
public DatabaseManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("We create table");
db.execSQL(TABLE_CREATE);
}
// public static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_CREATE + ";";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_USERS);
onCreate(db);
}
}
and here the one where I define the operations:
public class BDD {
private static final int VERSION = 1;
private static final String NOM_BDD = "users.db";
private static final String TABLE_USERS = "table_users";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_PASSWORD = "PASSWORD";
private static final int NUM_COL_PASSWORD = 1;
private static final String COL_JOUR = "JOUR";
private static final int NUM_COL_JOUR = 2;
private static final String COL_MOIS = "MOIS";
private static final int NUM_COL_MOIS = 3;
private static final String COL_DN = "DN";
private static final int NUM_COL_DN = 4;
private static final String COL_SEXE="SEXE";
private static final int NUM_COL_SEXE = 5;
private SQLiteDatabase bdd;
private DatabaseManager b;
public BDD(Context context) {
System.out.println("constructeur");
b = new DatabaseManager(context, NOM_BDD, null, VERSION);
}
public void openForWrite() {
bdd = b.getWritableDatabase();
}
public void openForRead() {
bdd = b.getReadableDatabase();
}
public void close() {
bdd.close();
}
public SQLiteDatabase getBdd() {
return bdd;
}
public long insertUser(User user) {
ContentValues content = new ContentValues();
content.put(COL_ID, user.getId());
content.put(COL_PASSWORD,user.getPassword());
content.put(COL_JOUR,user.getJour());
content.put(COL_MOIS,user.getMois());
content.put(COL_DN,user.getAnnee());
content.put(COL_SEXE,user.getSexe());
return bdd.insert(TABLE_USERS, null, content);
}
public int updateUser(int id, User user) {
ContentValues content = new ContentValues();
content.put(COL_ID, user.getId());
content.put(COL_PASSWORD,user.getPassword());
content.put(COL_JOUR,user.getJour());
content.put(COL_MOIS,user.getMois());
content.put(COL_DN,user.getAnnee());
content.put(COL_SEXE,user.getSexe());
return bdd.update(TABLE_USERS, content, COL_ID + " = " + id, null);
}
public User getUserbyid(String id) throws ParseException {
Cursor c = bdd.query(TABLE_USERS, new String[] { COL_ID, COL_PASSWORD,
COL_JOUR,COL_MOIS,COL_DN, COL_SEXE }, COL_ID + " LIKE \"" + id + "\"", null, null,
null, COL_ID);
return cursorToUser(c);
}
public User cursorToUser(Cursor c) throws ParseException {
if (c.getCount() == 0) {
c.close();
return null;
}
User user = new User();
user.setId(c.getString(NUM_COL_ID));
user.setPassword(c.getString(NUM_COL_PASSWORD));
user.setJour(c.getInt(NUM_COL_JOUR));
user.setMois(c.getInt(NUM_COL_MOIS));
user.setAnnee(c.getInt(NUM_COL_DN));
user.setSexe(c.getInt(NUM_COL_SEXE));
c.close();
return user;
}
}
Thanks in advance