2012-05-06 19 views
3

在我的應用程序中,我在很多情況下使用數據庫,但有一種情況是我得到一個異常,並非每次都可以重現(但)。SQLiteException:不是一個錯誤

這隻發生在操作系統版本2.3.7和2.1-update-1上。

代碼:

public void removeOldOccurrences() { 
     SQLiteDatabase db = dbHelper.getWritableDatabase(); 
     Long oldTime = System.currentTimeMillis() - VALID_OCCURRENCE_TIME; 
     String query = ""; 
     try { 
      query = "DELETE FROM " + LOCATIONS_TABLE + " WHERE not (" 
        + REMEMBERED_FIELD + "=1) " + "and (" + LAST_FIELD + "<" 
        + oldTime + ");"; 
      db.execSQL(query); 
     } catch (Exception e) { 
      Log.e(TAG, query); 
      e.printStackTrace(); 
     } finally { 
      if (db != null) { 
       db.close(); 
      } 
     } 
    } 

的異常跟蹤是:

android.database.sqlite.SQLiteException: not an error 
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 
at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1849) 
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820) 
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854) 
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847) 
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:573) 
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) 

請幫助。

回答

1

看起來您的查詢在not上不好。

看着SQLite Docs,根據他們沒有not在你試圖使用它的方式(作爲你的地方否定)。

您的選擇不似乎是:

NOT LIKE 
NOT GLOB 
NOT REGEXP 
NOT MATCH 
NOT BETWEEN 
NOT IN 
IS NOT 

而且大多數的他們之前需要表達。

WHERE xxxx NOT MATCH yyyy 
+0

嘿男人謝謝你的答案。我不認爲這是真的,有兩個原因:1.除了那幾個案例(大部分是在2.3.7)之外,同一個查詢每次都成功。 2.在您鏈接的同一文檔中寫入:「這些是一元前綴運算符: - +〜NOT」。除此之外,preetha說,並寫道,當他說我得到了getWritableDatabase()方法的異常。 – Bush

3

此錯誤來自您嘗試創建/打開數據庫的語句getWritableDatabase。

從代碼段你給了什麼,我看到你試圖打開每個操作的數據庫。

這不是正確的做法。在關閉數據庫之前,您只需打開一次數據庫。正常的做法是在您的其他初始化過程中打開它,並在退出應用程序之前注意關閉它。一旦開店鋪分貝背景和使用,在所有其他操作

爲例如:你可以有一個數據庫manegr類是這樣的:

public DataBaseManager open() throws SQLException { 
    try { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
     Utils.getHomeActivityinstance().finish(); 

    } 
    return this; 
} 

現在,當被稱爲delte需要,不要再調用open

public boolean deleteRow(String tableName, long rowId) { 

    return mDb.delete(tableName, ID + "=" + rowId, null) > 0; 
} 
+0

感謝您的回答,我明白這不是使用數據庫的有效方式,但我不明白您的解決方案在執行getWritableDatabase()時不會得到同樣的異常。正如我所提到的,我只在2.3.7和2.1-update-1上看到這個例外,而不是每次。 – Bush

+0

一個可能的問題可能是「打開已打開的數據庫」。用我的設計這個問題不會在我的情況下來 – png

0

我花了兩個小時尋找這個問題的解決方案,這是我無法在SQLite數據庫中創建表的原因。

我的錯誤是我有「is_selected」屬性,這導致不是錯誤的錯誤。重命名屬性,它工作。

14

傳遞空字符串進行查詢時,我也收到了同樣的錯誤。

+0

我只有空間,技術上不是空的,但結果相同 –

1

確保您的查詢不包含分號後的空行「;」您的查詢

我有同樣的問題在Sqlite客戶端內執行查詢後,發現問題是,我有兩個空行在查詢下面。

如果有人遇到同樣的問題,這裏是Sqlite客戶端內部的幾個通過/失敗案例(或加載.sql文件)。

好:

-- test.sql 
SELECT * FROM table; 

好:

-- test2.sql 
-- notice the empty line below doesn't break the code 

SELECT * FROM table; 

好:

-- test3.sql 
-- notice the query string is not terminated by semicolon ";" 
-- but it does contain trailing empty lines 
SELECT * FROM table 


-- this line is just a placeholder, in actual file this line should also be empty 

壞:

-- test3.sql 
-- notice the query string is terminated with semicolon, 
-- and there are trailing empty lines after that 
SELECT * FROM table; 


-- this line is just a placeholder, in actual file this line should also be empty 

希望這會有所幫助。

+0

好,趕上了,這幫了我。 – gotomanners

相關問題