spatiallite空间数据库在Android平台的两种数据查询方式

来源:互联网 发布:黑陶茶具含铅高 知乎 编辑:程序博客网 时间:2024/06/05 13:25

spatialite在Android平台的学习资料非常少,它的数据查询方式主要有异步式和非异步式两种。

异步式

        db = new jsqlite.Database();        File dbFile=new File(Environment.getExternalStorageDirectory().getPath()+"/test.sqlite");        db.open(dbFile.toString(), jsqlite.Constants.SQLITE_OPEN_READONLY);        String query = "SELECT name, peoples, AsText(Geometry) from Towns where peoples > 350000";        Callback cb = new Callback() {            @Override            public void columns(String[] coldata) {                Log.e(TAG, Arrays.toString(coldata));            }            @Override            public void types(String[] types) {                Log.e(TAG, "Types: " + Arrays.toString(types));            }            @Override            public boolean newrow(String[] rowdata) {                Log.e(TAG, rowdata[0]);                return false;            }        };        db.exec("select Distance(PointFromText('point(-77.35368 39.04106)', 4326), PointFromText('point(-77.35581 39.01725)', 4326));",                cb);        db.exec("SELECT AsGeoJSON(Geometry) from HighWays;",                cb);        db.exec("SELECT name from sqlite_sequence;",                cb);        db.exec("SELECT name, peoples, AsText(Geometry), GeometryType(Geometry), NumPoints(Geometry), SRID(Geometry), IsValid(Geometry) from Towns where peoples > 350000;",                cb);        db.exec("SELECT Distance( Transform(MakePoint(4.430174797, 51.01047063, 4326), 32631), Transform(MakePoint(4.43001276, 51.01041585, 4326),32631));",                cb);        db.close();
异步式查询是将查询的结果在回掉事件中输出,查询到一行输出一行。

非异步式

db = new jsqlite.Database();        File dbFile=new File(Environment.getExternalStorageDirectory().getPath()+"/test.sqlite");        db.open(dbFile.toString(), jsqlite.Constants.SQLITE_OPEN_READONLY);        String query = "SELECT name, peoples, AsText(Geometry) from Towns where peoples > 350000";        Stmt stmt = db.prepare(query);        stmt.step();        while (stmt.step()) {            String tableName = stmt.column_string(0);            String type = stmt.column_string(1);            String srid = stmt.column_string(2);            Log.e(TAG, tableName+type+srid);        }        stmt.close();        db.close();

非异步式则在主线程中一直判断是否有下一行,如果没有,则向后执行