Android SQLiteException“没有这样的表”

时间:2016-08-16 17:17:48

标签: c# android sqlite

当我尝试使用DBAdapter中的getFavorieten(favoriet)方法时出错。它说没有这样的桌子“Favorieten”,而我清楚地宣布它并且它在那里没有给我任何错误。

我有两个表,并为它们使用类似的代码。在我的第一张桌子“Gebruikers”上没有任何问题,但在我的桌子上“Favorieten”它似乎找不到我的桌子。这是我的DBAdapter的代码。我遗漏了无关紧要的东西。

public class DBAdapter {

//Gebruikers

static final String KEY_EMAIL = "email";
static final String KEY_WACHTWOORD = "wachtwoord";
static final String KEY_KADNR = "kadnr";
static final String TAG = "DBAdapter";


static final String DATABASE_NAME = "AndroidDB";
static final String DATABASE_TABLE_GEBRUIKERS = "Gebruikers";
static final String DATABASE_TABLE_FAVORIETEN = "Favorieten";

static final int DATABASE_VERSION = 6;

static final String CREATE_TABLE_GEBRUIKERS = "create table Gebruikers (email text primary key, wachtwoord text not null);";
static final String CREATE_TABLE_FAVORIETEN = "create table Favorieten (id integer primary key auto_increment, kadnr text not null, foreign key(gebruiker) references Gebruikers(email) on delete cascade);";

final Context context;

DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}




private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TABLE_GEBRUIKERS);
            db.execSQL(CREATE_TABLE_FAVORIETEN);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS Gebruikers");
        db.execSQL("DROP TABLE IF EXISTS Favorieten");
        onCreate(db);
    }

}


public DBAdapter open()
{

    db = DBHelper.getWritableDatabase();
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys = ON;");
    }
    return this;


}


public void close()
{
    DBHelper.close();
} 


public Gebruiker getGebruiker(Gebruiker gebruiker) throws SQLException
{
    String email = gebruiker.getEmail();
    Cursor mCursor = db.query(true, DATABASE_TABLE_GEBRUIKERS, new String[] {KEY_EMAIL, KEY_WACHTWOORD}, KEY_EMAIL
            + "='" + email + "'", null, null, null, null, null);

        mCursor.moveToFirst();
        if(mCursor.getCount()==1) {
            return createGebruiker(mCursor);
        }
        else {
            return null;
        }
}

public Favoriet getFavoriet(Favoriet favoriet) throws SQLException
{
    String kadnr = favoriet.getKadnr();
    String email = favoriet.getEmail();
    Cursor mCursor = db.query(true, DATABASE_TABLE_FAVORIETEN, new String[]{KEY_KADNR, KEY_EMAIL}, KEY_KADNR + "= ? AND " + KEY_EMAIL + "= ?", new String[]{kadnr, email}, null, null, null, null, null);

    mCursor.moveToFirst();
    if(mCursor.getCount()==1) {
        return createFavoriet(mCursor);
    }
    else {
        return null;
    }
}



private Gebruiker createGebruiker(Cursor mCursor) {
    Gebruiker gebruiker = new Gebruiker();
    gebruiker.setEmail(mCursor.getString(mCursor.getColumnIndex(KEY_EMAIL)));
    gebruiker.setWachtwoord(mCursor.getString(mCursor.getColumnIndex(KEY_WACHTWOORD)));
    return gebruiker;
}

private Favoriet createFavoriet(Cursor mCursor) {
    Favoriet favoriet = new Favoriet();
    favoriet.setKadnr(mCursor.getString(mCursor.getColumnIndex(KEY_KADNR)));
    favoriet.setEmail(mCursor.getString(mCursor.getColumnIndex(KEY_EMAIL)));
    return favoriet;
}




}

0 个答案:

没有答案