2012-04-24 60 views
1

我創建了一個標籤欄,當我第一次進入數據庫屏幕時出現這個代碼工作正常。但是,當我們去另一個選項卡上和數據庫屏幕選項卡上再去它拋出一個異常使用數據庫時出現「DatabaseIOException:文件系統錯誤(12)」?

net.rim.device.api.database.DatabaseIOException:文件系統錯誤(12)

我已經正確關閉數據庫。

我在最後block.Database是當我移動的標籤

這是我的代碼每次關閉關閉數據庫:

Database d = null; 
    URI _uri = null; 
    Statement st = null; 
    Cursor c = null; 

    try 
    { 

     _uri=URI.create("file:///SDCard/MyBudgetTracker.db"); 
      if (DatabaseFactory.exists(_uri)) { 
       d=DatabaseFactory.openOrCreate(_uri,new DatabaseSecurityOptions(false)); 
       st = d.createStatement("SELECT * FROM "+Globalvalue.planCategoryTable); 
       st.prepare(); 
       c = st.getCursor(); 
       Row r; 
       int i = 0; 

       while(c.next()) { 
        r = c.getRow(); 
        r.getString(0); 
        i++; 
       } 
       if (i==0) 
       { 
        add(new RichTextField("No data in the User table.")); 
       } 

     } 


    }catch (Exception e) 
    { 
     System.out.println(e.getMessage()); 
     System.out.println(e); 
     e.printStackTrace();// TODO: handle exception 
    } finally { 
     try { 
      if (DatabaseFactory.exists(_uri)) { 
       if (c != null) { 
        c.close(); 
       }if (st != null) { 
        st.close(); 
       } if (d != null) { 
        d.close(); 
       } 
      } 
     } catch (Exception e2) { 
      // TODO: handle exception 
     } 
    } 
+0

這是Android,不是? – 2012-04-24 06:58:14

+0

當你關閉遊標或語句時,你可能會遇到另一個錯誤。由於所有的結束代碼都在同一個try/catch中,所以你有機會不關閉數據庫本身。 – 2012-04-24 08:08:34

回答

-1

您檢索值到您選擇的標籤從數據庫? 您可以考慮打開和關閉數據庫的這種替代方法。它始終產生較小的錯誤。

try 
{  
    //Open or create the database 
    Database db = DatabaseFactory.openOrCreate("MyBudgetTracker.db");  
    //Retrieve Data from db 
    Statement statement1 = db.createStatement("SELECT * FROM "+Globalvalue.planCategoryTable); 
    statement1.prepare(); 
    statement1.execute();  
    statement1.close(); 
    db.close(); 
} 
catch(DatabaseException dbe) 
{ 
    System.err.println(dbe.toString()); 
} 

否則,您可以包括在每個回合標籤的分貝公開聲明ú取前值雖然它WUD增加烏爾編程代碼行。

+0

我不認爲這種方法更好。如果出現錯誤,Db可能無法正確關閉。 – 2012-04-25 07:59:54

相關問題