我正在尝试将经纬度坐标插入到 SQLite 表中,我得到了SQLiteConstraintException: error code 19: constraint failed
.
我有 try/catch 包围的代码,并且没有抛出异常。我在我的 logcat 中收到约束错误。
我已经附加了数据库类和来自我的主要活动的调用。任何帮助或指导将不胜感激!
以下是我的 logcat 的前几行:
08-25 14:39:06.934: E/SQLiteDatabase(681): Error inserting lat=59310767 longi=-8613281
08-25 14:39:06.934: E/SQLiteDatabase(681): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
08-25 14:39:06.934: E/SQLiteDatabase(681): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
08-25 14:39:06.934: E/SQLiteDatabase(681): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
08-25 14:39:06.934: E/SQLiteDatabase(681): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1745)
08-25 14:39:06.934: E/SQLiteDatabase(681): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1618)
这是我的 DbHelper 中的代码:
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_LAT + " REAL NOT NULL, " +
KEY_LONGI + " REAL NOT NULL)"
// KEY_NAME + " TEXT NULL, " +
// KEY_INFO + " TEXT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
这是我的数据库管理器
public DatabaseManager(Context c) {
ourContext = c;
}
public DatabaseManager open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDB = ourHelper.getWritableDatabase(); //example show writeable
return this;
}
public void close() {
ourHelper.close();
}
public long createPin(long latitude, long longitude) {
ContentValues cv = new ContentValues();
cv.put(KEY_LAT, latitude);
cv.put(KEY_LONGI, longitude);
return ourDB.insert(DATABASE_TABLE, null, cv);
}
这是来自我的 MainActivity 的调用:
try{
long longitude= touchedPoint.getLongitudeE6();
long latitude= touchedPoint.getLatitudeE6();
//Add Pin to the Database
DatabaseManager db = new DatabaseManager(MainActivity.this);
db.open();
db.createPin(latitude,longitude);
db.close();
}