0

我正在使用以下代码在数据库中插入一个项目。但每次我得到例外。HistoryItem 有成员:sectorCount=int; 链接=字符串;currentDownloadedBytes=long;

内部活动

DatabaseHelper helper=new DatabaseHelper(getApplicationContext());
helper.checkAndInserItem(new HistoryItem("http://dummy.com/dummy.jpg",4,2212),1000);

在 DatabaseHelper 内部

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_DOWNLOAD_ITEMS + "("
            + KEY_URL + " TEXT PRIMARY KEY," + KEY_NUMBER_OF_SECTORS_USED + " INTEGER,"
            + KEY_CURRENTLY_DOWNLOADED_BYTES + " TEXT"+")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}



public void checkAndAddItem(HistoryItem item,int maxNumberOfItemsAllowedInDb) {
   SQLiteDatabase db = this.getWritableDatabase();
   ContentValues values = new ContentValues();
   values.put(KEY_URL, item.link); //item.link is a string
   values.put(KEY_NUMBER_OF_SECTORS_USED, item.sectorCount); //item.sectorCount is an Integer
   values.put(KEY_CURRENTLY_DOWNLOADED_BYTES, ""+ item.currentlyDownloadedBytes );//item.currentlyDownloadedBytes is a long

   String selectQuery = "SELECT * FROM " + TABLE_DOWNLOAD_ITEMS;


   Cursor cursor = db.rawQuery(selectQuery, null);
   int k=cursor.getCount();
   if(k<maxNumberOfItemsAllowedInDb)
     db.insert(TABLE_DOWNLOAD_ITEMS, null, values);
   else
   {
     if(cursor.moveToLast()) {
          dummy=cursor.getString(0) ;
          db.delete(TABLE_DOWNLOAD_ITEMS, KEY_URL + " = ?",
                new String[] { dummy });
          db.insert(TABLE_DOWNLOAD_ITEMS, null, values);
          }  
     }
cursor.close();
db.close(); 

}

在 logcat 中:

09-07 16:47:43.532: E/SQLiteDatabase(14450): Error inserting currentlyDownloadedBytes=2212 numberOfSectorsUsed=4 url=http://dummy.com/dummy.jpg<
09-07 16:47:43.532: E/SQLiteDatabase(14450): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1839)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1712)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at com.rahul.SpeedyDownload.lib.DownloadDatabaseHelper.checkAndAddItem(DownloadDatabaseHelper. java:73)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at com.rahul.SpeedyDownload.lib.MultiConnectionDownloader$SectionDownloader.run(MultiConnectionDownloader.java:216)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-07 16:47:43.532: E/SQLiteDatabase(14450):    at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

0

此行正在创建错误

    values.put(KEY_CURRENTLY_DOWNLOADED_BYTES, ""+ item.currentlyDownloadedBytes )

改成

   values.put(KEY_CURRENTLY_DOWNLOADED_BYTES, ""+String.valueOf(item.currentlyDownloadedBytes) )

因为它被声明为 TEXT 而不是整数

于 2012-09-07T11:43:31.137 回答