简体   繁体   中英

Android sqlite insert is not inserting

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

请尝试使用insertOrThrow , - 如果您的数据库约束不允许特定插入,它将抛出异常,那么您将能够弄清楚。

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.

您尝试在“Longitutde”中插入,但您创建了“经度”字段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM