为什么这个SQLite数据库无法正常工作

时间:2017-02-26 11:40:54

标签: java android android-sqlite

我正在尝试为我的游戏添加前10名高分。高分仅由两个部分组成 - 分数和难度,但到目前为止,我不太了解这个数据库是如何工作的,但经过几个教程我完成了这个

public class DBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "highscores";

private static  final String TABLE_DETAIL = "scores";

private static  final String KEY_ID = "id";
private static  final String KEY_TIME = "time";
private static  final String KEY_DIFFICULTY = "difficutly";


public DBHandler(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION);}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_HIGHSCORES_TABLE = "CREATE TABLE " + TABLE_DETAIL + "("
            + KEY_ID + " INTEGER PRIMARY KEY, "
            + KEY_TIME + " TEXT, "
            + KEY_DIFFICULTY + " TEXT)";
    db.execSQL(CREATE_HIGHSCORES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAIL);     

    onCreate(db);                                          
}
// Adding new score
public void addScore(int score) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_TIME, score); // score value

    // Inserting Values
    db.insert(TABLE_DETAIL, null, values);

    db.close();

}

// Getting All Scores
public String[] getAllScores() {

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_DETAIL;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list

    int i = 0;

    String[] data = new String[cursor.getCount()];

    while (cursor.moveToNext()) {

        data[i] = cursor.getString(1);

        i = i++;

    }
    cursor.close();
    db.close();
    // return score array
    return data;
}
}

这是我想要从

控制数据库的类
public class highscores extends Activity {     

private ListView scorebox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    DBHandler db = new DBHandler(this);
    scorebox = (ListView) findViewById(R.id.scorebox);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscores);
    Log.d("Insert: ", "Inserting ..");
    db.addScore(9000);
    Log.d("Reading: ", "Reading all contacts..");
}
}

当我在应用程序中打开具有高分的页面时 - 它是空白的,如何使其显示某些内容,我尝试使用此命令db.addScore(9000);但它不起作用。也许我没有告诉数据库在哪里显示这些数据?

3 个答案:

答案 0 :(得分:0)

如果您正在使用模拟器,您可以拉取db文件并检查表是否已创建,如果您使用的是设备,则可以使用此命令

cd / D D:\ Android \ SDK \ platform-tools // android sdk path

adb -d shell

run-as com.pkg.pkgname

cat /data/data/com.pkg.pkgname/databases/highscores> / sdcard / highscores

它会将您的db文件复制到设备SD卡。使用Sqlite browser,您可以打开db文件并检查表是否已创建。 同时卸载应用程序并重新安装

答案 1 :(得分:0)

使用此功能在列表视图中列出您的分数

public class highscores extends Activity {     

    private ListView scorebox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscores);
        DBHandler db = new DBHandler(this);
        scorebox = (ListView) findViewById(R.id.scorebox);
        Log.d("Insert: ", "Inserting ..");
        db.addScore(9000);
        Log.d("Reading: ", "Reading all contacts..");
        String []s=db.getAllScores();
        ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
        scorebox.setAdapter(ar);
    }
    }

答案 2 :(得分:0)

编辑高分类

public class highscores extends Activity {     
    private ListView scorebox;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscores);
    DBHandler db = new DBHandler(this);
    scorebox = (ListView) findViewById(R.id.scorebox);
    Log.d("Insert: ", "Inserting ..");
    db.addScore(9000);
    Log.d("Reading: ", "Reading all contacts..");
    ArrayList<String>ar=new ArrayList<>();
    ar=db.getAllScores();
    ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
    scorebox.setAdapter(ar);
}
}

现在在你的DBHandler类中编辑像这样的getAllScores()方法

public ArrayList getAllScores() {

// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_DETAIL;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list


ArrayList<String>data=new ArrayList<>();

while (cursor.moveToNext()) {

    data.add(cursor.getString(1));


}
cursor.close();
db.close();
// return score array
return data;
}