5

I'm looking at the tutorial for Spatialite-Android, and I'm noticing the following code samples:

String query = "SELECT AsText(Transform(MakePoint(" + TEST_LON + ", " + TEST_LAT + ", 4326), 32632));";
sb.append("Execute query: ").append(query).append("\n");
try {
    Stmt stmt = db.prepare(query);
    if (stmt.step()) {
        String pointStr = stmt.column_string(0);
        sb.append("\t").append(TEST_LON + "/" + TEST_LAT + "/EPSG:4326").append(" = ")//
                .append(pointStr + "/EPSG:32632").append("...\n");
    }
    stmt.close();
} catch (Exception e) {
    e.printStackTrace();
    sb.append(ERROR).append(e.getLocalizedMessage()).append("\n");
}

In particular, I noticed that poor practice is done of simply stringing together a SQL query, instead of a more proper method, such as is used by the Android SQLite library. Is there a way that I can make Spatialite use true prepared statements?

Just to be clear, I'm looking for something like this, using the standard SQLite database in Android:

String query="SELECT * FROM table WHERE _id=?";
Cursor data=db.rawQuery(query,new String[]{id});
4

1 回答 1

5

有一些技巧。他们都使用 exec() 调用,该版本有 3 个参数。源代码中的语句是:

public void exec(String sql, jsqlite.Callback cb, String args[])

一个 jsqlite.Callback 是一个接口,其中可以有几个。但最好的方法似乎是使用一个db.get_table(query,args)函数。是Android SQLite 表示中%q的有效替代品。?这是给定代码的转换:

String query = "SELECT AsText(Transform(MakePoint(%q, %q, 4326), 32632));";
TableResult result=db.get_table(query,new String[]{""+TEST_LONG,""+TEST_LAT});

从那里,您只需要从 TableResult 中获取结果。没有从这里获取结果的方法调用,您实际上必须获取公开声明的变量并手动解析它。这是一个如何做到这一点的例子。

TableResult result=db.get_table(query,new String[]{""+lng,""+lat});
Vector<String[]> rows=result.rows;
for (String[] row:rows)
{
    for (String val:row)
    {
        Log.v(TAG,val);
    }
}

如果您不进行选择,请尝试以下操作:

TableResult result=new TableResult();
db.exec("ATTACH DATABASE %q AS newDb",result,new String[]{path});

我认为相同的模式将适用于 INSERTS 等

于 2013-11-16T17:28:21.060 回答