无法在Android SQLite上制作CRUD

时间:2016-04-13 12:04:56

标签: android database android-sqlite

我试图创建一个能够将高分保持在本地数据库中的游戏,如果最后得分大于db中的得分,我会更新它。但是有一点我无法弄清楚的问题。我无法从db获取值,而cursor.getCount()始终返回0。我希望在创建数据库的最开始时,高分的初始值为0。

这是我的代码:



package com.example.recepinanc.countdowntimer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by recepinanc on 29/03/16.
 */

public class DBHelper extends SQLiteOpenHelper {

    private static String DATABASE_NAME = "database_name";
    private static String TABLE_NAME = "table_name";
    private static String PLAYER_NAME = "player_name";
    private static String PLAYER_HIGHSCORE = "player_high_score";
    private static String PLAYER_ID = "player_id";

    private static String CREATE_PLAYER_TABLE = "CREATE TABLE " + TABLE_NAME + "( " + PLAYER_ID + " INTEGER PRIMARY KEY, " + PLAYER_NAME + " TEXT, " + PLAYER_HIGHSCORE + " INTEGER DEFAULT 0)";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_PLAYER_TABLE);
    }

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

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

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

    public void insertHighScore(int score) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PLAYER_HIGHSCORE, score);

        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public int getHighScore() {

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[]{PLAYER_HIGHSCORE}, null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
            Log.i("Get Count : ", cursor.getCount() + "");
        }

        return cursor.getInt(2);
    }

    public void updateHighScore(int newHighScore) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("UPDATE " + TABLE_NAME + " SET " + PLAYER_HIGHSCORE + "=" + newHighScore + " WHERE " + PLAYER_ID + "=1");
        Log.i("UPDATE","UPDATED !");
        db.close();
    }
}

public class GameOver extends Activity {

    DBHelper dbhelper;
    static int highScore;

    TextView best;
    ImageView repeat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_over_layout);

        dbhelper = new DBHelper(getApplicationContext());
        highScore = dbhelper.getHighScore();

        repeat = (ImageView) findViewById(R.id.play_again);
        best = (TextView) findViewById(R.id.best_score_tv);
        best.setText(dbhelper.getHighScore()+"");

        int score = getIntent().getIntExtra(MainActivity.CLICK_COUNTS, 0);

        dbhelper.insertHighScore(score);

        if (score > highScore) {
            dbhelper.updateHighScore(score);
            Log.i("HighScoreNew", dbhelper.getHighScore() + "");
        }

        repeat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GameOver.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView clickCountTextView = (TextView) findViewById(R.id.clickCounts_TextView);
        clickCountTextView.setText(score + "");
    }
}




提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要先启动交易,然后将其标记为成功并结束。下面给出了如何插入的示例:

 try {  
       SQLiteDatabase db = this.getWritableDatabase();
       ContentValues values = new ContentValues();
       db.beginTransaction();
       values.put(PLAYER_HIGHSCORE, score);
       db.insert(TABLE_NAME, null, values);
       db.setTransactionSuccessful();
     } 
finally {
       db.endTransaction();
   }