SQLite - 无法检索所有值 - getString(int index)无法正常工作

时间:2014-12-19 08:49:41

标签: android database sqlite android-sqlite sqliteopenhelper

在我的项目中,我有一个名为places的表格如下:

places(int id auto increment, string placename, double latitude, double longitude, string placedetails)

现在,我创建了两个类,如下所示:

Place.java (适用于模型(getter和setter))

public class Place {

    private String mname;
    private double mlatitude,mlongitude;
    private String mplacedetails;

    public Place(String name, double latitude, double longitude,String placedetails)
        {

          mname = name;
          mlatitude = latitude;
          mlongitude = longitude;
          mplacedetails = placedetails;


    }

    public Place() {

    }


    public void setname(String placename)
    {
        mname = placename;


    }

    public String getname()
    {
        return mname;


    }

    public void setlatitude(double latitude)
    {
        mlatitude=latitude;


    }

    public double getlatitude()
    {
        return mlatitude;


    }


    public void setlongitude(double longitude)
    {
        mlongitude = longitude;


    }

    public double getlongitude()
    {
        return mlongitude;


    }


    public void  setPlacedetails(String placedetails)
    {

        mplacedetails = placedetails;
    }


    public String getplacedetails()
    {
        return mplacedetails;
    }


}

MySDQLiteHelper.java (扩展SQLiteOpenHelper的类)

public class MySqliteHelper extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "MyApplicationDatabase";

    private static final String KEY_ID = "id";
    private static final String KEY_PLACENAME = "placename";
    private static final String KEY_PLACEDETAILS = "placedetails";
    private static final String KEY_LATITUDE = "latitude";
    private static final String KEY_LONGITUDE = "longitude";

    private static final String TABLE_NAME = "places";


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


    }

    @Override
    public void onCreate(SQLiteDatabase db) {



        //actual query = create table places (id primary key autoincrement, placename taxt, latitude real, longitude real);
        String query =  "CREATE TABLE " +TABLE_NAME + "( " + KEY_ID+
                " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PLACENAME+
                " TEXT, "+ KEY_PLACEDETAILS+
                " TEXT, "+ KEY_LATITUDE+
                " REAL, "+KEY_LONGITUDE+ " REAL)";


                db.execSQL(query);
                Log.d("my", "Successfully created table: " + query);



    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS places");

        this.onCreate(db);
    }


    public void addPlaces(Place place)
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues convalues = new ContentValues();



        convalues.put(KEY_PLACENAME,place.getname());
        convalues.put(KEY_LATITUDE,place.getlatitude());
        convalues.put(KEY_LONGITUDE,place.getlongitude());
        convalues.put(KEY_PLACEDETAILS,place.getplacedetails());

          db.insert(TABLE_NAME, null, convalues);
          Log.d("my","db.insert(TABLE_NAME, null, convalues)");
          Log.d("my", "Values inserted");
          db.close();

    }

    public Place getPlace(int id) {
        SQLiteDatabase  db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, KEY_LATITUDE, KEY_LONGITUDE,
                        KEY_PLACENAME, KEY_PLACEDETAILS }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

       Place place = new Place(cursor.getString(1),
                cursor.getDouble(2), cursor.getDouble(3),cursor.getString(4));

        return place;
    }

    public List<Place> getAllPlaces() {
        List<Place> placeList = new ArrayList<Place>();



        SQLiteDatabase db = this.getWritableDatabase();

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;

        Cursor cursor = db.rawQuery(selectQuery, null);
        Log.d("my","query fired");
        Log.d("my",cursor.toString());

        if (cursor.moveToFirst()) {

            Log.d("my","in movetofirst()");

            do {

                Log.d("my","in do");
                Place place = new Place();

                place.setname(cursor.getString(1));
                Log.d("my","place name");
                place.setlatitude(cursor.getDouble(2));
                place.setlongitude(cursor.getDouble(3));
                place.setPlacedetails(cursor.getString(4));

                placeList.add(place);
            } while (cursor.moveToNext());
        }


        return placeList;
    }



}

每次点击按钮时,我都会通过活动致电addPlace

        mpostplacename = (EditText) findViewById(R.id.edittext_placename);


        mpostplacedetails = (EditText) findViewById(R.id.edittext_placedetails);

        gps = new GPSTracker(RememberActivity.this);


        if(gps.canGetLocation()) {

            mylatitude = gps.getLatitude();
            mylongitude = gps.getLongitude();




            Toast.makeText(getApplicationContext(), "Your Location is - \nLatitude: " + mylatitude + "\nLongitude: " + mylongitude, Toast.LENGTH_LONG).show();


            myplacename = mpostplacename.getText().toString();
            myplacedetails = mpostplacedetails.getText().toString();

            if(myplacename==null)
            {
                  myplacename = mylatitude+"   "+mylongitude;

            }
            if(myplacedetails==null)
            {
                    myplacedetails = "No details given about this place";
            }
            MySqliteHelper db = new MySqliteHelper(this);


            db.addPlaces(new Place(myplacename, mylatitude, mylongitude, myplacedetails));

当我想在Log中显示存储的数据时:

 MySqliteHelper db = new MySqliteHelper(this);
    List<Place> places = db.getAllPlaces();

    for (Place p : places) {
        String log = "name:" + p.getname() + " ,details: " + p.getplacedetails();

        Log.d("my", log);
    }

在插入placename和placedetails(两者都作为一些字符串值,如blablabla等)后,日志显示地名正确但位置细节错误:

D/my﹕ name:sdsdsda ,details: 0
D/my﹕ name:aaaaaaaaaaa ,details: 0

为什么getplacedetails无效?我在光标索引中找不到任何错误。可能是索引中的错误...因为它无法在索引上找到位置详细信息。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

尝试这种方式来创建Place对象并在placeList方法中添加getAllPlaces(...)

   placeList.add(new Place(cursor.getString(1),cursor.getDouble(2),cursor.getDouble(3),cursor.getString(4)));

也会更改 Place.java

public class Place {

private String mname;
private double mlatitude,mlongitude;
private String mplacedetails;

public Place(String name, double latitude, double longitude,String placedetails)
    {
      this.mname = name;
      this.mlatitude = latitude;
      this.mlongitude = longitude;
      this.mplacedetails = placedetails;
 }

public void setname(String placename)
{
    this.mname = placename;
}

public String getname()
{
    return mname;
}

public void setlatitude(double latitude)
{
    this.mlatitude=latitude;
}

public double getlatitude()
{
    return mlatitude;
}

public void setlongitude(double longitude)
{
    this.mlongitude = longitude;
}

public double getlongitude()
{
    return mlongitude;
}

public void  setPlacedetails(String placedetails)
{
    this.mplacedetails = placedetails;
}

public String getplacedetails()
{
    return mplacedetails;
}

}

答案 1 :(得分:2)

替换如下代码行:

place.setname(cursor.getString(1));

有这样的事情:

place.setname(cursor.getString(cursor.getColumnIndex(KEY_PLACENAME)));

看起来与KEY_PLACEDETAILS对应的列实际上是第2列,并且您尝试从第4列获取内容。无论如何,使用cursor.getColumnIndex()来获得与每个键匹配的列更安全,以防您的数据库稍后更改并帮助避免此类错误。