-1

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

  • 1
    I guess you had some tries before where the database was created. If you have any changes in the database structure, like adding or removing columns, you have to delete the existing database. Uninstall and reinstall your app and try again. – Opiatefuchs May 24 '17 at 16:55
  • 3
    Incomplete information. Where is the table created? Where is the code that inserts? About the data types, it is well explained on [the official documentation](https://www.sqlite.org/datatype3.html). – m0skit0 May 24 '17 at 16:56
  • @m0skit0 I've just edited my post. About the data types, I had found this link too but was a bit surprised to hear that some "classical" SQL types were not managed –  May 24 '17 at 17:02
  • 1
    The point of SQLite is to be small, simple and fast enough. If you start adding types, you will complicate the engine. – m0skit0 May 24 '17 at 17:07
  • post the database class implementation – Ali Ahsan May 24 '17 at 17:07
  • Still missing the code where you're calling the table creation. Where do you use `TABLE_CREATE`? – m0skit0 May 24 '17 at 17:08
  • @AliAhsan Post edited –  May 24 '17 at 17:12
  • @m0skit0 Post edited, thanks for explication on types –  May 24 '17 at 17:12
  • it seems that @Prakash answer work –  May 24 '17 at 17:14

1 Answers1

0

I think you did some changes in the columns. Uninstall the app from phone or emulator and install again. It will work.

Prakash
  • 34
  • 1
  • 4