执行rawQuery时出现SQLiteException - 语法错误

时间:2014-03-20 14:48:51

标签: android android-sqlite

执行以下 rawQuery 时,我收到SQLiteException / SQL语法错误

代码:

public static final String NAME = "name";   
public static final String LONG = "longitude";   
public static final String LAT = "latitude";
public static final String IDENTNR = "identNr";
public static final String IDENTSTR = "ident";
public static final String TABLE_NAME = "communities";

public Cursor findClosest(double lng, double lat) {

     Cursor c = db.rawQuery("SELECT " + NAME + ", " + LAT + ", " + LONG + ", " + IDENTNR + ", " + IDENTSTR + " SQRT(" +
                "POW(69.1 * (latitude - " + lat + "), 2) + " +
                "POW(69.1 * (" + lng + " - longitude) * COS(latitude / 57.3), 2)) AS distance " +
                "FROM " + TABLE_NAME + " HAVING distance < 25 ORDER BY distance DESC", null);

     return c;
}

Logcat错误消息:

03-20 15:46:07.248: E/AndroidRuntime(20605): FATAL EXCEPTION: main
03-20 15:46:07.248: E/AndroidRuntime(20605): android.database.sqlite.SQLiteException: near "(": syntax error (code 17): , while compiling: SELECT name, latitude, longitude, identNr, ident SQRT(POW(69.1 * (latitude - 48.2347197), 2) + POW(69.1 * (16.3550203 - longitude) * COS(latitude / 57.3), 2)) AS distance FROM communities HAVING distance < 25 ORDER BY distance DESC
03-20 15:46:07.248: E/AndroidRuntime(20605):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

不幸的是,我对SQL语句不是很熟悉,也无法弄清楚我的语法错误可能在哪里?

SQL中是否存在SQRT()POW()COS()函数,我是否正确使用它们?

我的表名绝对正确,我的数据库/表包含值。

2 个答案:

答案 0 :(得分:1)

据我所知,SQLite中不存在这些函数。但是,您可以添加功能。有关详细信息,请参阅this StackOverflow帖子。

答案 1 :(得分:1)

首先,请更正:

IDENTSTR + ", " + " SQRT(.....