2013-01-17 55 views
0

我很難得到任何插入與SqlLite一起使用。我有一個用下面的模式創建的表: create table drink_category(_id integer not null primary key autoincrement,value unique);Sqlite insertOrThrow()失敗,數據庫文件未找到錯誤

當附加的代碼運行時,我得到一個SQLiteCantOpenDatebaseException,並顯示錯誤消息「無法打開數據庫文件(代碼14)」。

正如您在代碼中看到的那樣,我使用查詢訪問表時沒有問題。我也做了db.isOpen()來驗證數據庫是否正常。我甚至在插入塊之後添加了代碼,一切正常。任何幫助將不勝感激。

public Boolean add(String strValue) 
{ 
    Boolean bStatus = true; 
    String  strSql; 
    String  sqlValue = strValue.replaceAll("'","\'\'"); 

    if (bStatus) 
    { 
     if (strValue.isEmpty()) 
     { 
      BarDB.getInstance().setErrorMessage("No value entered."); 
      bStatus = false; 
     } 
    } 
    if (bStatus) 
    { 
       strSql = "select value from " + "drink_category" + " where value = '" + sqlValue + "' "; 
     Cursor cursor = SqlDbAdapter.getInstance().getDatabase().rawQuery(strSql, null); 

     if (null == cursor) 
     { 
      BarDB.getInstance().setErrorMessage("Problem with the database."); 
      bStatus = false; 

     } 
     else if (cursor.getCount() > 0) 
     { 
      BarDB.getInstance().setErrorMessage("A Drink Category with that name already exists."); 
      bStatus = false; 
     } 
    } 
    if (bStatus) 
    { 
     ContentValues cvValues = new ContentValues(); 
     cvValues.put("value", sqlValue); 
     SQLiteDatabase db = SqlDbAdapter.getInstance().getDatabase(); 

     if (db.isOpen()) 
     { 
      try 
      { 
       long lerror = db.insertOrThrow("drink_category", null, cvValues); 
      } 
      catch (SQLiteException sqle) 
      { 
       BarDB.getInstance().setErrorMessage(sqle.toString()); 
       bStatus = false; 
      } 
     } 
    } 
    return bStatus; 
} 
+0

發現它:我在模擬器上運行它,並將數據庫文件存儲在/ data/data中。我想當你做一個插入時,sqlite需要寫入一個日誌文件,並且該目錄是隻讀的。我將數據庫移至/ data/data/ /數據庫,現在工作正常。更好的錯誤信息會更有幫助! – user1988510

回答

0

發現它:我在模擬器上運行它,並將數據庫文件存儲在/ data/data中。我想當你做一個插入時,sqlite需要寫入一個日誌文件,並且該目錄是隻讀的。我將數據庫移至/ data/data //數據庫,現在工作正常。更好的錯誤信息會更有幫助!

相關問題