我的Sqlite表有什么问题

时间:2017-05-24 16:53:48

标签: android database sqlite

我在Android中创建了一个SqLite数据库,并出现以下错误:

  

(1)没有这样的表:table_users
   插入DN = 1 JOUR = 1 ID = aa SEXE = 0 PASSWORD = bb MOIS = 1

这是SQL代码:

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 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);
}

}

这里是我定义操作的那个:

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;
}
}

提前致谢

1 个答案:

答案 0 :(得分:0)

我认为您在列中做了一些更改。从手机或模拟器卸载应用程序并重新安装。它会起作用。

相关问题