无效的int""在SQLiteDatabase

时间:2014-10-18 21:09:40

标签: java android database sqlite

我正在制作一个小应用程序,因此我需要使用一个小的SQLite数据库。

请帮助我,我不知道如何解决此错误:

在第34行的SQLiteDatabase中,db = this.getReadableDatabase();"在数据库中 ,(Main:int teams = db.countAllTeams())

错误:W / System.err:无效的int:""

数据库:

public class DatabaseHelper extends SQLiteOpenHelper {

  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "database";
  private static final String TABLE_TEAM = "team";

  private static final String KEY_TEAM_FULLNAME = "full_name";
  private static final String KEY_TEAM_SHORTNAME = "short_name";
  private static final String KEY_TEAM_STADIUM = "stadium";
  private static final String KEY_TEAM_LOGO = "logo";

  private static final String CREATE_TABLE_TEAM = "CREATE TABLE "
        + TABLE_TEAM + "(" + KEY_TEAM_FULLNAME + " TEXT PRIMARY KEY," +
                             KEY_TEAM_SHORTNAME + " TEXT," +
                             KEY_TEAM_STADIUM + " TEXT," +
                             KEY_TEAM_LOGO + " INTEGER" + ")";


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

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

@Override
public void onUpgrade(SQLiteDatabase db, int old, int neW) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEAM);
    onCreate(db);
}

public void closeDB() {
    SQLiteDatabase db = this.getReadableDatabase();
    if (db != null && db.isOpen()) db.close();
}

public long createTeam(Team team) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TEAM_FULLNAME, team.getFullName());
    values.put(KEY_TEAM_SHORTNAME, team.getShortName());
    values.put(KEY_TEAM_LOGO, team.getLogo());
    values.put(KEY_TEAM_STADIUM, team.getStadium());

    return db.insert(TABLE_TEAM, null, values);
}

public int countAllTeams(){
    SQLiteDatabase db = this.getReadableDatabase();
    List<Team> teams = new ArrayList<Team>();

    return Integer.getInteger(db.compileStatement("SELECT COUNT(*) FROM " + TABLE_TEAM).simpleQueryForString());
}

}

主要

DatabaseHelper db;
TextView tvText;

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

    tvText = (TextView)findViewById(R.id.tvText);
    String text = "";

    db = new DatabaseHelper(getApplicationContext());
    int teams = db.countAllTeams();

    tvText.setText(teams);
}
}

3 个答案:

答案 0 :(得分:1)

Integer.getInteger不会将字符串转换为整数。 从查询中获取单个数字的最简单方法是使用DatabaseUtils

long count = DatabaseUtils.longForQuery(db,
                "SELECT COUNT(*) FROM " + TABLE_TEAM, null);

但要获得表格中的行数,可以实现更简单的功能:

long count = DatabaseUtils.queryNumEntries(db, TABLE_TEAM);

此外,当您给TextView.setText一个整数时,它需要一个资源ID。 您必须手动将计数转换为字符串:

tvText.setText(Integer.toString(teams));

答案 1 :(得分:0)

.setText不直接接受整数,而是尝试:

tvText.setText(String.valueOf(teams));

Integer value in TextView

答案 2 :(得分:0)

我使用rawQuery()代替simpleQueryForString()

public int countAllTeams(){
    SQLiteDatabase db = this.getReadableDatabase();
    List<Team> teams = new ArrayList<Team>();

    Cursor c = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_TEAM, new String[]{});
    c.moveToFirst();

    return c.getInt(0);
}