SQLite数据库不返回任何行

时间:2016-05-12 16:44:01

标签: android mysql sqlite

对于我的应用程序,我试图将高分保存到我的Android设备上名为“highscores.db”的数据库中。现在我遇到一个问题,即mysql没有在al处返回任何行,所以我猜想我的插入错误或我的检索正在进行。我根本不知道什么是错的(第一次使用sqlite用户)。

DBHandler.java

public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "highscores.db";
public static final String TABLE_HIGHSCORES = "highscores";
public static final String COLUMN_WAVE = "wave";
public static final String COLUMN_MONEY = "money";
public static final String COUKN_ZOMBIEKILL = "zombiekill";

public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_HIGHSCORES + "(" + COLUMN_WAVE + " INTEGER, " +
            COLUMN_MONEY + " DOUBLE, " +
            COUKN_ZOMBIEKILL + " INTEGER " + ")";
    db.execSQL(query);
}

//Upgrade db
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_HIGHSCORES);
    onCreate(db);
}

//add new row
public boolean addHighScore(Highscore highscore) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_WAVE, highscore.getWave());
    values.put(COLUMN_MONEY, highscore.getMoneyLeftOver());
    values.put(COUKN_ZOMBIEKILL, highscore.getZombiekill());
    SQLiteDatabase db = this.getWritableDatabase();
    long result = db.insert(TABLE_HIGHSCORES, null, values);
    if(result == -1){
        return false;
    }else{
        return true;
    }
}

//Get database info
public ArrayList<Highscore> getHighScores(){
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM highscores";

    int wave = 0;
    double money = 0;
    int zombiekill = 0;
    Highscore hs = null;
    ArrayList<Highscore> highscores = new ArrayList<>();
    Cursor c = db.rawQuery(query,null);
    c.moveToFirst();

    while(c.moveToNext()){
        if(c.getString(c.getColumnIndex("wave")) != null){
            wave = Integer.parseInt(c.getString(c.getColumnIndex("wave")));
        }
        if(c.getString(c.getColumnIndex("money")) != null){
            money = Integer.parseInt(c.getString(c.getColumnIndex("money")));
        }
        if(c.getString(c.getColumnIndex("zombiekill")) != null){
            zombiekill = Integer.parseInt(c.getString(c.getColumnIndex("zombiekill")));
        }
        hs = new Highscore(wave,money,zombiekill);
        highscores.add(hs);
    }

    db.close();
    return highscores;
}

}

此外,我不知道在哪里查看此数据库以检查是否正在填充。许多人给出了可以找到它的解释,但我不知道如何到达那里。

〜提前致谢

EDIT1.0更改了公共ArrayList getHighScores()

    public ArrayList<Highscore> getHighScores(){
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM highscores";

    int wave = 0;
    double money = 0;
    int zombiekill = 0;
    Highscore hs = null;
    ArrayList<Highscore> highscores = new ArrayList<>();
    Cursor c = db.rawQuery(query,null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("wave")) != null){
            wave = Integer.parseInt(c.getString(c.getColumnIndex("wave")));
        }
        if(c.getString(c.getColumnIndex("money")) != null){
            money = Integer.parseInt(c.getString(c.getColumnIndex("money")));
        }
        if(c.getString(c.getColumnIndex("zombiekill")) != null){
            zombiekill = Integer.parseInt(c.getString(c.getColumnIndex("zombiekill")));
        }
        hs = new Highscore(wave,money,zombiekill);
        highscores.add(hs);
        c.moveToNext();
    }

    db.close();
    return highscores;
}

仍然无法正常工作

EDIT2.0:这是我处理保存的方式

 DBHandler handler = new DBHandler(getApplicationContext(),null,null,1);
    for (int i = 0; i < 5; i++) {
        handler.addHighScore(new Highscore(10,30,55));
    }

这是我处理在不同文件上检索的方式:

    DBHandler handler = new DBHandler(getActivity().getApplicationContext(), null, null, 1);
    HighScoreSingleton.getInstance().setHighscores(handler.getHighScores());

1 个答案:

答案 0 :(得分:0)

感谢BenjamínMolina,我发现我正在处理输出错误。

我只需要更新代码的处理。 更新,在同一个java类上进行导入和选择,或者创建一个单例,它将跟踪调用dbhandler的tehs ame实例。

DBHandler handler = new DBHandler(getActivity().getApplicationContext(), null, null, 1);
    for (int i = 0; i < 5; i++) {
        handler.addHighScore(new Highscore(10,30,55));
    }

    HighScoreSingleton.getInstance().setHighscores(handler.getHighScores());

然而,我仍然发现,如果我关闭应用程序并重新启动它而不首先将新数据添加到数据库中,则所有数据都将丢失,如何从先前创建的数据库中检索该数据。