【问题标题】:Sqlite insertOrThrow() failing with database file not found errorSqlite insertOrThrow() 失败,找不到数据库文件错误
【发布时间】:2013-01-17 21:45:02
【问题描述】:

我很难让任何插入与 SqlLite 一起使用。我有一个使用以下架构创建的表: 创建表drink_category(_id整数不为空主键自增,值唯一);

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

【问题讨论】:

  • 发现它:我在模拟器上运行它,我将数据库文件存储在 /data/data.我猜当您进行插入时,sqlite 需要写入日志文件并且该目录是只读的。我将数据库移至 /data/data//databases,现在一切正常。更好的错误消息会更有帮助!

标签: sqlite


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多