0

我正在尝试计算两个位置之间的距离。一个位置的纬度现在是硬编码的(我稍后将通过考虑 gps 的值来更改它)其他位置存储在数据库中。我正在使用以下查询

Cursor c = sampleDB.rawQuery("SELECT NAME, CONTACT,((LAT -" +
        " LATCUR) * (LAT - LATCUR) + ((LONG - LONGCUR) * 2) * ((LONG - LONGCUR) * 2)) " +
        "AS DISTANCE FROM AMBULANCE ORDER BY DISTANCE", null );

其中LATCURLONGCUR是当前值。我收到错误,因为没有名为 latcur 的列。我该怎么办?

我将 latcur 和 longcur 值声明为 public static double LATCUR = 39.991932; 公共静态双 LONGCUR = -83.012352;

代码:公共类 MainActivity 扩展 ListActivity {

private final String DBNAME = "SERVICES";
private final String TABLENAME = "AMBULANCE";
public static double LATCUR = 39.991932;
public static double LONGCUR = -83.012352;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ArrayList<String> results = new ArrayList<String>();
    SQLiteDatabase sampleDB = null;

    try {

        sampleDB =  this.openOrCreateDatabase(DBNAME, MODE_PRIVATE, null);

        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                TABLENAME +
                " (NAME VARCHAR, CONTACT VARCHAR," +
                " LAT float, LONG float, LATC double, LONGC double);");

Cursor c = sampleDB.rawQuery("SELECT NAME, CONTACT,((LAT -" + " LATCUR) * (LAT - LATCUR) + ((LONG - LONGCUR) * 2) * ((LONG - LONGCUR) * 2)) " + "AS DISTANCE FROM AMBULANCE ORDER BY DISTANCE", null );

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String NAME = c.getString(c.getColumnIndex("NAME"));
                    String CONTACT = c.getString(c.getColumnIndex("CONTACT"));
                    results.add("" + NAME + ",CONTACT: " + CONTACT);
                }while (c.moveToNext());
            } 
        }

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));


    }   
4

2 回答 2

1

You have them quoted. You need to do all your math outside the quoted SQL string.

于 2013-03-30T11:54:25.173 回答
1

try this.....

    Cursor c = sampleDB.rawQuery("SELECT NAME, CONTACT,((LAT -"+LATCUR+") * (LAT - "+LATCUR+") + ((LONG - "+LONGCUR+") * 2) * ((LONG - "+LONGCUR+") * 2)) " +
 "AS DISTANCE FROM AMBULANCE ORDER BY DISTANCE", null );

hope it helps.

于 2013-03-30T12:27:16.097 回答