Android:sqlite记录插入一次但不是两次

时间:2013-07-09 19:24:09

标签: android sqlite insert

我正在尝试将“播放器名称”添加到我的数据库中的表中。它正好插入第一个,然后在第二个名称上崩溃。

public void addTeam1Members(){         // TODO自动生成的方法存根

    EditText playername, playerinit;
    DBAccessMatch dbAccess = new DBAccessMatch(this);

    String pname, pinit;
    dbAccess.open();
    for (int x = 0; x < team1Players; x++) {
        switch (x) {
        case 0:

            playername = (EditText) findViewById(R.id.etTeam1Player1);
            GlobalVars.setTeam1PlayerNames(playername.getText().toString(),
                    x);
            GlobalVars.sT1P1 = playername.getText().toString();
            playerinit = (EditText) findViewById(R.id.etTeam1Player1Init);
            GlobalVars.setTeam1PlayerInit(playerinit.getText().toString(),
                    x);
            if (playername.getText().toString().trim().isEmpty() == true) {
                pname = " ";
            } else {
                pname = playername.getText().toString().trim();
            }
            if (playerinit.getText().toString().trim().isEmpty() == true) {
                pinit = " ";
            } else {
                pinit = playerinit.getText().toString().trim();
            }
            Toast.makeText(getApplicationContext(), pname + " - " + pinit, Toast.LENGTH_SHORT).show();
            dbAccess.createPlayer(pname, pinit);
            break;
        case 1:
            playername = (EditText) findViewById(R.id.etTeam1Player2);
            GlobalVars.setTeam1PlayerNames(playername.getText().toString(),
                    x);
            GlobalVars.sT1P2 = playername.getText().toString();
            playerinit = (EditText) findViewById(R.id.etTeam1Player2Init);
            GlobalVars.setTeam1PlayerInit(playerinit.getText().toString(),
                    x);
            if (playername.getText().toString().trim().isEmpty() == true) {
                pname = " ";
            } else {
                pname = playername.getText().toString().trim();
            }
            if (playerinit.getText().toString().trim().isEmpty() == true) {
                pinit = " ";
            } else {
                pinit = playerinit.getText().toString().trim();
            }
            Toast.makeText(getApplicationContext(), pname + " - " + pinit, Toast.LENGTH_SHORT).show();
            dbAccess.createPlayer(pname, pinit);
            break;

这是显示玩家1和玩家2的一小部分代码。除了他们所居住的编辑文本框之外,它们最终是完全相同的。

当createPlayer(name,initial)在第一次完美插入时运行,然后第二次没有。

    public void createPlayer(String playername, String playerinitials) {

    try {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PLAYERNAME, playername);
        cv.put(KEY_PLAYERINITIALS, playerinitials);
        ourDatabase.insert(DATABASE_TABLE3, null, cv);

    } catch (Exception e) {
        String error = e.toString();
        Dialog d = new Dialog(null);
        d.setTitle("Dang it!");
        TextView tv = new TextView(null);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }

}

1 个答案:

答案 0 :(得分:0)

在对该问题进行一些研究后,我无法确定错误是什么(使用logcat)。我在其他地方移动了“创建用户”功能,并在此课程之外完成,一切正常。