6

i have this little gps locaton inserting code but thats not insert into the database:

db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);

final String gps = "CREATE TABLE IF NOT EXISTS GPS_Values ("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT, Latitude float(10, 6), Longitude float(10, 6), cur_timestamp TIMESTAMP);";
db.execSQL(gps);

and the inserting lines:

// GPS
public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();

        ContentValues gps_values = new ContentValues();

        gps_values.put("Latitude", loc.getLatitude());
        gps_values.put("Longitutde", loc.getLongitude());


        try {
            db.beginTransaction();
            db.insert("GPS_Values", null, gps_values);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        String Text = "My current location is: " + "Latitude = "
                + loc.getLatitude() + "\nLongitude = " + loc.getLongitude();

        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();

    }

    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Disabled",
                Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

}// gps vége

when tring to get location everithing works fine but the inserting.

i have similar code with tha accelerator and that works fine.

pls help me

3 Answers 3

16

Try using insertOrThrow instead, - it will throw an exception if your db constraints don't allow a specific insertion, then you will be able to figure out.

Sign up to request clarification or add additional context in comments.

1 Comment

saved my day! insertOrThrow throws an error and i know exactly what went wrong
9

There a typo in the column you're inserting to:

    gps_values.put("Longitutde", loc.getLongitude());

Should be

    gps_values.put("Longitude", loc.getLongitude());

The insert() call returns -1 if an error occurred, but you aren't checking the return value. It's generally easier to use insertOrThrow instead, which throws an exception on error.

3 Comments

and that's because nothing inserting?
Yes, if you try to insert to a non-existent column it will generate an error.
ok...now it's working :) THX!!! but it's just with i don't know how to call it, so here's an example: 41.2211 <- only 4 after the 41.:)
0

You try to insert in "Longitutde" but you've create "Longitude" field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.