android SQLite无法存储或检索数据

时间:2013-02-19 07:57:02

标签: android sql database sqlite

我试图在数据库中存储一些评论然后检索它们并在textview中显示它们但是当我尝试访问我的数据库时,通过游标得到0的计数。我没有错误,应用程序工作正常。

这是从数据库添加和检索数据的代码

  public class DatabaseHandler extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;
     private static final String DATABASE_NAME = "ReviewManager ";
     private static final String TABLE_REVIEWS = "Review";
     private static final String KEY_ID = "reference";
     private static final String KEY_NAME = "review";

    public DatabaseHandler(Context context) {

        super(context, null, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String CREATE_REVIEW_TABLE="CREATE TABLE " + TABLE_REVIEWS +"(" + KEY_ID + " TEXT," + KEY_NAME +" TEXT" + ")";
        db.execSQL(CREATE_REVIEW_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        // TODO Auto-generated method stub
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_REVIEWS);
            onCreate(db);
    }

    // Adding new contact
    public void addReview(Reviews r) {
         SQLiteDatabase db = this.getWritableDatabase();
         ContentValues values = new ContentValues();
         values.put(KEY_ID, r.getReference());
         values.put(KEY_NAME, r.getReview());
         db.insert(TABLE_REVIEWS, null, values);
         db.close();
    }


    public List<Reviews> getAllReviews(String reference) {
        SQLiteDatabase db = this.getWritableDatabase();

        List<Reviews> reviewList = new ArrayList<Reviews>();
        String selectQuery = "SELECT  * FROM " + TABLE_REVIEWS + " WHERE " + KEY_ID + "='" + reference+"'";
        Cursor cursor = db.rawQuery(selectQuery, null);
        if(cursor.getCount()!=0)
        {
        if (cursor.moveToFirst()) {
            do {
                Reviews review = new Reviews();
                review.setReference(cursor.getString(0));
                review.setReview(cursor.getString(1));
                // Adding contact to list
                reviewList.add(review);
            } while (cursor.moveToNext());
        }
        }
        else {
            Log.d("no data", "no data");
        }
        cursor.close();
        return reviewList;

    }


}

这是我主要活动中的代码,我尝试显示数据..

 final TextView tv = (TextView) findViewById(R.id.review);

        Log.d("Reading: ", "Reading all contacts..");
        List<Reviews> reviews = db.getAllReviews(reference);
        for (Reviews r : reviews)
        {
            tv.append(r.getReview()+"\n");
        }

        final EditText et = (EditText) findViewById(R.id.typereview);
        Button b = (Button) findViewById(R.id.addreview);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String review = et.getText().toString();
                 Log.d("Insert: ", "Inserting ..");
                 db.addReview(new Reviews(reference,review));
                 tv.append(review+ "\n");

            }
        });

0 个答案:

没有答案