java Android sqlite insert 未插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11634444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android sqlite insert is not inserting
提问by David
i have this little gps locaton inserting code but thats not insert into the database:
我有这个小的 gps 定位插入代码,但那不是插入到数据库中:
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.
我有与 tha 加速器类似的代码,并且工作正常。
pls help me
请帮助我
回答by Alexander Kulyakhtin
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.
尝试使用insertOrThrow
, - 如果您的数据库约束不允许特定插入,它将引发异常,然后您将能够弄清楚。
回答by Graham Borland
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.
该insert()
调用返回-1,如果发生错误,但你不检查返回值。insertOrThrow
相反,它通常更容易使用,它会在出错时引发异常。
回答by Alix Bloom
You try to insert in "Longitutde" but you've create "Longitude" field
您尝试插入“经度”,但您已创建“经度”字段