database handling in MapActivity

Viewed 71

I have created a apk with Google Map API. In it's MainActivity it has two buttons. First shows my location on google map, and saves latitude and longitude to SQLite database. Second button opens the google map, with all the places loaded from database (those are places where I previously checked my location).

I created the application in the way that two those MainActivity buttons are launching same MapActivity, but with different flag. In onMapReady methode I used If statement where I am chcecking the flag to know which button is calling.

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));
    }
}
}

In my MainActivity's onCreate() methode I have created Database like this

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);

The part which shows my location works fine, but when I want to check all my places application crashes, with error message.

#################################################################
 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

Is it due to database locking?

1 Answers

Solve the problem. I changed the select query to

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

And I closed the db connection in both cases.

Related