SQlite database not storing double value as string

爱⌒轻易说出口 提交于 2021-01-29 05:41:44

问题


I am using following code

     public boolean addArea(AreaClass area , ArrayList<AreaMarkClass> areaArray)
     {      
         area.id =  getNextAreaId();
        Log.d("longitude", area.longitude);
        Log.d("latitude", area.latitude);

        ContentValues initialValues = new ContentValues();
        initialValues.put("_id", area.id);
        initialValues.put("name", area.name);
        initialValues.put("longitude", area.longitude);
        initialValues.put("latitude", area.latitude);
        initialValues.put("zoomLevel", area.zoomLevel);
        initialValues.put("creationDate",area.creationDate);

        try
        {
            mDb.insertOrThrow("TTArea", null, initialValues);
}

area.longitude is a string whose value is obtained by String.value (A Double Value) Now usually it is something like this 22.323434523464563456

however when i try to retrieve this value

public AreaClass getAreaClassWithAreaId(Integer id)
{
    String queryString = "SELECT * FROM TTArea WHERE _id = ?";
    Cursor resultSet =  mDb.rawQuery(queryString, new String[]{id.toString()}); 
    AreaClass area = new AreaClass();
    while(resultSet.moveToNext())
    {
         area = new AreaClass(
                        resultSet.getInt(resultSet.getColumnIndex("_id")),
                        resultSet.getString(resultSet.getColumnIndex("name")),
                        resultSet.getString(resultSet.getColumnIndex("longitude")),
                        resultSet.getString(resultSet.getColumnIndex("latitude")),
                        resultSet.getString(resultSet.getColumnIndex("zoomLevel")),
                        resultSet.getString(resultSet.getColumnIndex("creationDate"))
                        );
    }

    resultSet.close();
    return area;
}

it somehow rounds it to 22.3234. In my app i need precise what i am storing. What is this behavior and how can i resolve this?


回答1:


I use these methods when working with SQLite:

public static Double stringToDouble (String x)
{
    if (x !=null)
        return Double.parseDouble(x);

    return null;
}

public static String doubleToString (Double y)
{
    if (y != null)
        return String.valueOf(y);

    return null;
}

When adding to database transform the double value to string and vice-versa, and please tell me if this solution works

EDIT: The solution is in comments: "I see you created your table with STRING fields. You can try with TEXT type, works for me."



来源:https://stackoverflow.com/questions/14295475/sqlite-database-not-storing-double-value-as-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!