Android中的SQL数据库不起作用

时间:2015-06-04 16:04:47

标签: java android sql database android-sqlite

我试图创建一个包含3列名称,胜利和损失的数据库。 数据表应该只有一行 - 只有一个玩家。 我试图这样做,但它不起作用,应用程序总是崩溃。 我有2个活动和一个普通班级。在主要活动中,它检查玩家姓名是否在数据库中,如果不是,则要求他输入他的名字。 在第二个活动(GameActivity)中,它假设向我展示他的名字以及他赢得并输给计算机的时间。那里有2个按钮,添加胜利和增加损失,每个按钮应该增加赢或输的数量。 sql数据库类扩展了SQLiteOpenHelper

MainActivity

SQLGameTable db;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.leaderboards);
    db = new SQLGameTable(this);
    if(db.getName() == "")
    {
        final Score score = new Score("", 0, 0);
        Button send = (Button) findViewById(R.id.btnSend);
        final EditText et = (EditText) findViewById(R.id.editText1);
        send.setVisibility(android.view.View.VISIBLE);
        et.setVisibility(android.view.View.VISIBLE);
        send.setOnClickListener(new OnClickListener()
        {       
            @Override
            public void onClick(View v)
            {
                String name = et.getText().toString();
                score.setName(name);
            }
        });
        db.addPlayer("Daniel", score.getWins(), score.getLoses());
        send.setVisibility(android.view.View.INVISIBLE);
        et.setVisibility(android.view.View.INVISIBLE);
    }
    final Intent i = new Intent(this, GameActivity.class);
    btn.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        {
            startActivity(i);
        }
    });
}

GameActivity

Button aWin, aLose;
SQLGameTable scoreTable;
TextView tv1, tv3, tv4;
Score score;
String name;
int win=0, lose=0;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    scoreTable = new SQLGameTable(this);
    aWin = (Button) findViewById(R.id.button1);
    aLose = (Button) findViewById(R.id.button2);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv3 = (TextView) findViewById(R.id.textView3);
    tv4 = (TextView) findViewById(R.id.textView4);
    name = scoreTable.getName();
    win = scoreTable.getWin();
    lose = scoreTable.getLose();
    tv1.setText(name + "");
    tv3.setText(win + "");
    tv4.setText(lose + "");
    aLose.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            lose++;
            scoreTable.reset();
            scoreTable.addPlayer(name, win, lose);
            lose = scoreTable.getLose();
            tv4.setText("" + (lose));
        }
    });
    aWin.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            win++;
            scoreTable.reset();
            scoreTable.addPlayer(name, win, lose);
            win = scoreTable.getWin();
            tv3.setText("" + (win));
        }
    });
}

SQLGameTable扩展了SQLiteOpenHelper

public static String DATABASE_NAME = "highscores";
public static String TABLE_NAME = "scores";
public static String KEY_ID = "id";
public static String KEY_NAME = "name";
public static String KEY_WIN = "win";
public static String KEY_LOSE = "lose";
public SQLGameTable(Context c) 
{
    super(c, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) 
{
    String sql = "CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, win INTEGER, lose INTEGER)";
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{

}

public void addPlayer(String name, int win, int lose) 
{
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name);
    values.put(KEY_WIN, win);
    values.put(KEY_LOSE, lose);
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_NAME, null, values);
    db.close();
}
public void reset() 
{
    SQLiteDatabase db = getWritableDatabase();
    db.delete(TABLE_NAME, null, null);
    db.close();
}
public String getName() 
{
    String sql = "SELECT * FROM " + TABLE_NAME;
    SQLiteDatabase db = getWritableDatabase();
    Cursor c = db.rawQuery(sql, null);
    int row = c.getColumnIndex(KEY_NAME);
    db.close();
    return c.getString(row);
}
public int getWin() 
{
    String sql = "SELECT * FROM " + TABLE_NAME;
    SQLiteDatabase db = getWritableDatabase();
    Cursor c = db.rawQuery(sql, null);
    int row = c.getColumnIndex(KEY_WIN);
    db.close();
    return Integer.parseInt(c.getString(row));
}
public int getLose() 
{
    String sql = "SELECT * FROM " + TABLE_NAME;
    SQLiteDatabase db = getWritableDatabase();
    Cursor c = db.rawQuery(sql, null);
    int row = c.getColumnIndex(KEY_LOSE);
    db.close();
    return Integer.parseInt(c.getString(row));
}

0 个答案:

没有答案