Sqlite insertOrThrow()失败,数据库文件未找到错误

时间:2013-01-17 21:45:03

标签: sqlite

我很难让任何插件与SqlLite一起使用。我有一个使用以下模式创建的表: create table drink_category(_id integer not null primary key autoincrement,value unique);

当附加的代码运行时,我得到一个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;
}

1 个答案:

答案 0 :(得分:0)

发现它:我在模拟器上运行它,我将数据库文件存储在/ data / data中。我想当你做插入时,sqlite需要写入日志文件,该目录是只读的。我将数据库移动到/ data / data //数据库,现在一切正常。更好的错误消息会更有帮助!

相关问题