繁体   English   中英

Android sqlite insert不插入

[英]Android sqlite insert is not inserting

我有这个小gps locaton插入代码但不插入数据库:

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

和插入行:

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

当获得位置的时候,工作正常,但插入。

我有加速器类似的代码,工作正常。

请帮助我

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

您要插入的列中有拼写错误:

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

应该

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

如果发生错误, insert()调用返回-1,但您没有检查返回值。 通常使用insertOrThrow更容易,这会在出错时抛出异常。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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