0

我将向微调器显示 sqlite db 中的数据。显示很容易,但我无法检索添加到 db 的数据。这是我的代码;

public class MainActivity extends Activity {

private Spinner spinner;
private SQLiteDBOlustur sqlnesne;
private SQLiteDatabase db;
private ContentValues cv;
private Cursor cursor;
private ArrayList<String> arr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinnerIller);
    arr = new ArrayList<String>();
    veriEkle();

    db = sqlnesne.getReadableDatabase();
    cursor = db.rawQuery("select * from iller", null);

    if (cursor.moveToFirst()) {
        do {
            arr.add(cursor.getString(cursor.getColumnIndex("adsoyad")));
        } while (cursor.moveToPrevious());
    }


    for (int i = 0; i < arr.size(); i++) {
        System.out.println(arr.get(i));
    }

}

只有我可以看到最后添加的数据。那么如何获取我的 sqlite db 中的所有数据?

这些也是添加数据的方法。

  private void veriEkle() {
   sqlnesne = new SQLiteDBOlustur(getApplicationContext());
   db = sqlnesne.getWritableDatabase();
   cv = new ContentValues();

       cv.put("il_adi", "Yalova");
   cv.put("il_adi", "Karabük");
   cv.put("il_adi", "Kilis");
   cv.put("il_adi", "Osmaniye");
   cv.put("il_adi", "Düzce");

    try {
        db.insert("iller", null, cv);
        Toast.makeText(getApplicationContext(), "Veriler eklendi!",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4

3 回答 3

1

尝试替换cursor.moveToPrevious()为,cursor.moveToNext()以便您通过光标前进而不是后退。

于 2013-08-31T01:44:30.313 回答
0

尝试这个

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

    spinner = (Spinner) findViewById(R.id.spinnerIller);
    arr = new ArrayList<String>();

    veriEkle();

        String Table_Name="name of table";

        String selectQuery = "SELECT  * FROM " + Table_Name;
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);
        String[] data = null;

        if (cursor.moveToFirst()) {
            do {
               // get  the  data into array,or class variable
              // process your data here.
            } while (cursor.moveToNext());
        }
}

希望它会帮助你。

于 2013-08-31T05:29:39.703 回答
0
if (cursor != null && cursor.moveToFirst()) {
        while (cursor.isAfterLast() == false) {
            arr.add(cursor.getString(cursor.getColumnIndex("adsoyad")));
            cursor.moveToNext();
        }
    }   
    cursor.close();

试试这个方法!!

于 2013-08-31T04:19:50.937 回答