getCount()在android中的SQLite中不起作用

时间:2014-10-08 08:01:30

标签: android android-sqlite

作为我的应用程序的一部分,我必须将问题和答案插入到android中的数据库中并检索它们并动态添加到布局中。 我能够插入数据库但无法从中检索。 此外,当表中只有5个元组时,getCount()方法返回133。 我的代码是 -

public class DataBaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Answers";
private static final String TABLE_OOPBASICS = "oopBasics";
private static final String KEY_QUESID = "ques_id";
private static final String QUESTION = "question";
private static final String ANSWER = "answer";

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

public void onCreate(SQLiteDatabase db) {
    String CREATE_OOPBASICS_TABLE = 
            "CREATE TABLE " + TABLE_OOPBASICS+ "("
            + KEY_QUESID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
            + QUESTION + " VARCHAR(300),"
            + ANSWER + " VARCHAR(100)" +")";
    db.execSQL(CREATE_OOPBASICS_TABLE);
     Log.d(tag,"rows inserted");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_OOPBASICS);
    onCreate(db);
}


// Adding new QUESTION
void addQuestion(Questions ques) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(QUESTION, ques.getQuestion()); 
    values.put(ANSWER, ques.getAnswer()); 
    db.insert(TABLE_OOPBASICS, null, values);
    db.close(); 
}
// Getting questions Count
public int getTotalRows() {
    int count=0;
    String countQuery = "SELECT * FROM " + TABLE_OOPBASICS; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    if (cursor != null) 
    {
        count = cursor.getCount(); 
        cursor.close();
         Log.d(tag,"in cursor closed");
    } 
    return count; 
    }

    public String getQuesWithId(int i) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = null; 
    String ques = ""; 
    String cols[]={KEY_QUESID,QUESTION};
    try{
        cursor = db.query(TABLE_OOPBASICS,cols,KEY_QUESID+"=i",null,null,null,null);
        if(cursor.getCount() > 0)
        { 
            cursor.moveToFirst(); 
            ques = cursor.getString(cursor.getColumnIndex("question"));
            } 
         Log.d(tag,"at get questn closing");
        return ques; 
        }

    finally {
        cursor.close();
    }
    }  

public String getAnswer(int i) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = this.getReadableDatabase();
    String cols[]={ANSWER};
    Cursor cursor = db.query(TABLE_OOPBASICS,cols,KEY_QUESID+"=i",null,null,null,null);
    if (cursor != null)
        cursor.moveToFirst();
    String ans= cursor.getString(6); 
    return ans;

}

}

并动态添加到布局

public class JavaHome extends Activity {
DataBaseHandler db;
SQLiteDatabase sdb;
int n;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_java_home);
    db=new DataBaseHandler(this);
    sdb=db.getWritableDatabase();
    db.addQuestion(new Questions("To which type of paradigm does Java belong    to?","OOP"));
    Log.d(tag,"1 row inserted");

db.addQuestion(new Questions("Which command is used to compile Java code?","javac"));
db.addQuestion(new Questions("What should be the name of Java class file ?","Class name"));
 db.addQuestion(new Questions("Which control structure exists in Java which does not exist in C and C++?","for-each"));
 db.addQuestion(new Questions("What is runtime polymorphism called?","Overriding")); 

     int i=db.getTotalRows();
    for( n=0;n<i;n++){
        LinearLayout ll=new LinearLayout(this);
        String ques=db.getQuesWithId(n);
        String ans=db.getAnswer(n);
        TextView tvq=new TextView(this);
        tvq.setText(ques);
        TextView tva=new TextView(this);
        tva.setText(ans);
        ll.addView(tvq);
        ll.addView(tva);
        setContentView(ll);

    }
}

public class Questions {
int _quesno;
String question;
String answer;

public Questions(String question,String answer){
    this.question=question;
    this.answer=answer;
}

public String getQuestion()
{
    return this.question;
}
public String getAnswer()
{
    return this.answer;
}

}

1 个答案:

答案 0 :(得分:2)

  当只有5个元组

时,

getCount()方法返回133

每次运行应用时,此数字是否会增加? 我认为每次运行应用程序时,代码都会向数据库中插入更多条目。如果是这种情况,请重新安装您的应用并为您的条目添加常量ID。

长期思考你应该熟悉ContenProvider作为处理数据库的有效方法。

请参阅: http://developer.android.com/guide/topics/providers/content-providers.html