2014-05-22 80 views
-1

我正面臨有關sqlite數據庫的問題。我想將數據保存在數據庫中。 KEY_LATITUDE和KEY_LONGITUDE分別從info.getLatitude()和info.getLongitude()獲得空值。但info.getLongitude()和info.getLatitude()分別爲12.99999和50.9999。如何在android中的sqlite中保存double數據類型值

private static final String KEY_ID = "id"; 
private static final String KEY_EventName = "event_name"; 
private static final String KEY_Description = "description"; 
private static final String KEY_Location = "location"; 
private static final String KEY_Date = "date"; 
private static final String KEY_Time = "time"; 
private static String KEY_LATITUDE ; 
private static String KEY_LONGITUDE ; 

addinfo函數將數據保存到數據庫中。

public void addInfo(Event info) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_EventName, info.getEventName()); 
    values.put(KEY_Location, info.getLocationName()); 

    values.put(KEY_Description, info.getDescription()); 
    values.put(KEY_Date, info.getDate()); 
    values.put(KEY_Time, info.getTime()); 

    values.put(KEY_LATITUDE, info.getLatitude()); // it return Double 
                // value.i.e 12.999999 
    values.put(KEY_LONGITUDE, info.getLongitude()); // it return Double 
                // value.i.e 12.999999 

    db.insert(TABLE_EVENT_DATA, null, values); 
    db.close(); // Closing database connection 
} 

回答

1

Double值轉換爲String

values.put(KEY_LATITUDE, String.valueOf(info.getLatitude())); 
values.put(KEY_LONGITUDE, String.valueOf(info.getLongitude())); 
1

需要初始化您的列名的變量:

private static String KEY_LATITUDE ; 
private static String KEY_LONGITUDE ; 

變化如

private static final String KEY_LATITUDE = "latitude"; 
private static final String KEY_LONGITUDE = "longitude"; 
相關問題