MapActivity中的数据库处理

时间:2017-11-27 13:46:49

标签: java android sqlite google-maps

我创建了一个包含Google Map API的apk。在它的MainActivity中它有两个按钮。首先显示我在谷歌地图上的位置,并将经度和经度保存到SQLite数据库。第二个按钮打开谷歌地图,从数据库加载所有地方(这些地方我之前检查过我的位置)。

我创建应用程序的方式是两个MainActivity按钮启动相同的MapActivity但具有不同的标志。在onMapReady方法中,我使用了If语句,我在chcecking标志,知道哪个按钮正在调用。

public void onMapReady(GoogleMap googleMap) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {

    mMap = googleMap;

if (getIntent().getFlags() == 1){
    MarkerOptions options = new MarkerOptions();
    LatLng llList;
    ArrayList<LatLng> resultList = new ArrayList<LatLng>();

    SQLiteDatabase mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.OPEN_READWRITE, null);
    mDatabase.setLocale(Locale.getDefault());
    String columns[] = {"lat", "lon"};
    Cursor c = mDatabase.query("latlon", columns, null, null, null, null, "result DESC");
    while (c.moveToNext())
    {
        Double lat = c.getDouble(c.getColumnIndex("lat"));
        Double lng = c.getDouble(c.getColumnIndex("lon"));
        try
        {
            llList = new LatLng(lat,lng);
            resultList.add(llList);
        }
        catch (Exception e) {
        }

    }
    for (LatLng point : resultList) {
        options.position(point);
        options.title(point.latitude + " " + point.longitude);
        mMap.addMarker(options);
    }
    LatLng center= new LatLng(11,11);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(center));

}
else{
    mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
        double lat = location.getLatitude();
        double lng = location.getLongitude();

        // Saving to DB
        ContentValues values = new ContentValues();
        values.put("lat", lat);
        values.put("lon", lng);
        long newAuthorID = mDatabase.insert("latlon", null, values);

        mMap.setMyLocationEnabled(true);
        // Add a marker in Sydney and move the camera
        LatLng place = new LatLng(lat,lng);
        mMap.addMarker(new MarkerOptions().position(place).title("Your location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
    }
}
}

在我的MainActivity的onCreate()方法中,我创建了像这样的数据库

locale = Locale.getDefault();
SQLiteDatabase mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.CREATE_IF_NECESSARY, null);
mDatabase.setLocale(locale);
String CREATE_LL_TABLE = "CREATE TABLE IF NOT EXISTS latlon (lat DOUBLE, lon DOUBLE);";
mDatabase.execSQL(CREATE_LL_TABLE);

显示我的位置的部分工作正常,但是当我想检查我的所有地方应用程序崩溃时,会显示错误消息。

#################################################################
 Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: result (code 1): , while compiling: SELECT lat, lon FROM latlon ORDER BY result DESC)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:996)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:561)
11at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1471)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1357)
at com.example.lukasz.aaaaa.MapsActivity.onMapReady(MapsActivity.java:75)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at gl.b(:com.google.android.gms.DynamiteModulesB@11518436:20)
at com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB@11518436:5)
at com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB@11518436:5)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W ActivityManager:   Force finishing activity com.example.lukasz.aaaaa/.MapsActivity

是否由于数据库锁定?

1 个答案:

答案 0 :(得分:0)

解决问题。 我将选择查询更改为

Cursor c = mDatabase.rawQuery("select * from latlon",null);

我在两种情况下都关闭了数据库连接。