[英]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.