2012-05-06 44 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

相关问题