在我的 android 应用程序中,我有一个存储在 \assets\database\myApp.sqlite 下的 sqlite 数据库
在应用程序加载时,我将检查 myApp.sqlite 文件(数据库)是否在手机中可用,如果不可用,我会将其存储在特定位置。
我的应用程序:
我将检索用户的当前位置并将这些坐标传递给下面提到的查询。此查询将在 myApp.sqlite(我的本地数据库)中搜索距用户当前点最近的位置,并检索最近的单个位置。
查询以查找距特定点最近的位置:
SELECT *, MIN( (latitude - USER LATITUDE) * (latitude - USER LATITUDE) + (USER LONGITUDE- longitude)*(USER LONGITUDE- longitude) ) AS DISTANCE FROM my_nearest_location_table ORDER BY DISTANCE
我的问题是:
上述查询仅在某些设备中正常工作。但是,当我尝试在 HTC ONE V 等其他设备中执行我的应用程序(相同的查询)时,会检索到其他一些错误的值(位置)。我不确定上述查询调用或从我的本地数据库中读取记录是否有问题。
为了测试,我还加载了这个本地数据库并在 SQLite Manager(FireFox 插件)中测试了上述相同的查询。正确检索最近的位置值。只有在某些特定设备(如 HTC One V)中,才会检索到三星 Duos 错误值。
我的代码如下:
public final String DATABASE_NAME="myApp.sqlite";
database = new MySQLiteHelper(context, DATABASE_NAME, null, 1).getWritableDatabase();
BASE_URL = "data/data/com.myApp.xxxx/databases/"+DATABASE_NAME;
//To get nearest area from user's location
public List<Location> getRecordsByLatAndLong(Context theContext, double theLat, double theLng)
{
String aSelectQuery;
List<Location> aLocationList = new ArrayList<Location>();
aSelectQuery = "SELECT *, MIN((latitude - "+theLat+") * (latitude - "+theLat+")" +
" + ("+theLng+"- longitude)*("+theLng+"- longitude)) AS DISTANCE " +
"FROM train_stations ORDER BY DISTANCE";
Cursor aCursor = database.rawQuery(aSelectQuery, null);
try
{
if (aCursor.moveToFirst())
{
do {
Location aLocation = new Location();
aLocation.setLocationId(aCursor.getInt(0));
aLocation.setLocationName(aCursor.getString(1));
aLocation.setLocationCode(aCursor.getString(2));
aLocation.setLat(aCursor.getDouble(3));
aLocation.setLng(aCursor.getDouble(4));
aLocationList.add(aLocation);
} while(aCursor.moveToNext());
}
aCursor.close();
}
catch (Exception theException) {
System.out.println("Exception..."+ theException);
}
return aLocationList;
}
谁能指导我为什么从特定数据库(对于相同的查询)检索结果时存在这种不一致。
提前致谢。