0

我正在为 Android 应用程序处理一些 SQLite 数据库。我有一个包含多个玩家的“玩家”表,以及一个包含每个玩家技能点的一对多“技能”表,例如投篮和篮板。

我在应用程序中有一项活动,用于实际填写文本框并将播放器插入数据库。当用户点击“添加玩家”按钮时,会在“玩家”表中插入一行,在“技能”表中插入一行,该表具有引用“玩家”表的外键。在这些插入之后,我进行了查询以检查是否可以从“技能”表中读取“射击”值并将其放入 Toast 通知中。效果很好,我使用的代码在这里:

SQLiteDatabase db2 = dbHelper.getReadableDatabase();

String[] projection = { "shooting" };

String sortOrder = "shooting" + " DESC";

Cursor c = db2.query(
    "skills",       // The table to query
    projection,     // The columns to return
    null,           // The columns for the WHERE clause
    null,           // The values for the WHERE clause
    null,           // don't group the rows
    null,           // don't filter by row groups
    sortOrder       // The sort order
);

c.moveToFirst();

int shooting = c.getInt(c.getColumnIndexOrThrow("shooting"));

Toast.makeText(this, "" + shooting, Toast.LENGTH_SHORT).show();

在我看到这有效后,我将其注释掉并放入一个 Intent 以使应用程序在插入玩家和技能后切换到“名册”活动。在'名册'活动中,我想获得每个球员的'射门'技能。当我使用上面完全相同的代码(适用于其他活动)时,我收到一条错误消息:

06-16 15:59:42.602: E/AndroidRuntime(31537): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.silverray.messaround/com.silverray.messaround.Roster}: java.lang.IllegalArgumentException: 列“射击”不存在

当我知道我将它包含在我的 SQL Create 语句中时,我无法弄清楚为什么它说“射击”列不存在,而且我什至能够从另一个活动中用相同的代码读取这个完全相同的列。

谢谢阅读。有任何想法吗?

编辑:这是名册活动的完整代码:

public class Roster extends Activity {

    int teamID = 0;

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

        // CHECK ROSTER

        DatabaseContract dbContract = new DatabaseContract();
        DatabaseContract.DbHelper dbHelper = dbContract.new DbHelper(this);

        SQLiteDatabase dbCheck = dbHelper.getReadableDatabase();

        Intent intent = getIntent();
        int ID = intent.getIntExtra("ID", 1);
        teamID = ID;

        String stringID = String.valueOf(ID);

        String[] projection = { "_id, playerFirstName, playerLastName, playerPosition" };
        String sortOrder = "playerFirstName" + " ASC";

        Cursor c = dbCheck.query(
            "player",
            projection,
            null,
            null,
            null,
            null,
            sortOrder
        );

        c.moveToFirst();

        int rowsAffected = c.getCount();

        if (rowsAffected < 1) {
            TextView rosterList = (TextView) findViewById(R.id.txtListRoster);
            rosterList.setText("Your team doesn't have any players!");
            c.close();
            dbCheck.close();
        } else {
            String players = "";

            for (int l = 0; l < rowsAffected; l++) {            
                String playerName = c.getString(c.getColumnIndexOrThrow("playerFirstName"));
                String playerLastName = c.getString(c.getColumnIndexOrThrow("playerLastName"));     
                String position = c.getString(c.getColumnIndexOrThrow("playerPosition"));
                int playerID = c.getInt(c.getColumnIndexOrThrow("_id"));        
                String player_ID = String.valueOf(playerID);
                String pos = "";

                if (position.equals("Point Guard")) {
                    pos = "PG";
                } else if (position.equals("Shooting Guard")) {
                    pos = "SG";
                } else if (position.equals("Small Forward")) {
                    pos = "SF";
                } else if (position.equals("Power Forward")) {
                    pos = "PF";
                } else if (position.equals("Center")) {
                    pos = "C";
                }

                SQLiteDatabase db2 = dbHelper.getReadableDatabase();

                String[] projection2 = { "shooting" };

                String sortOrder2 = "shooting" + " DESC";

                Cursor c2 = db2.query(
                    "skills",
                    projection2,
                    null,
                    null,
                    null,
                    null,
                    sortOrder2
                );

                c2.moveToFirst();

                //** Everything works until this line:

                int shooting = c2.getInt(c.getColumnIndexOrThrow("shooting"));

                players += playerName + " " + playerLastName + " (" + pos + ") Shooting: ";

                if (l != (rowsAffected - 1)) {
                    players += "\n";
                }

                TextView rosterList = (TextView) findViewById(R.id.txtListRoster);
                rosterList.setText(players);

                if (l != (rowsAffected - 1)) {
                    c.moveToNext();
                }

                c2.close();
            }

            c.close();
            dbCheck.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.roster, menu);
        return true;
    }

    public void addPlayer(View view) {

        Intent goToAddPlayer = new Intent(this, AddPlayer.class);
        goToAddPlayer.putExtra("ID", teamID);
        this.startActivity(goToAddPlayer);
        this.finish();
        return;
    }
}
4

1 回答 1

0
int shooting = c2.getInt(c.getColumnIndexOrThrow("shooting"));

应该

int shooting = c2.getInt(c2.getColumnIndexOrThrow("shooting"));

您现在正在处理第二个查询,但尝试从第一个获取列索引。

于 2013-06-16T21:02:39.700 回答