SQLite在频繁的“选择"操作中无法打开数据库文件(代码14).询问 [英] SQLite unable to open database file (code 14) on frequent "SELECT" query

查看:576
本文介绍了SQLite在频繁的“选择"操作中无法打开数据库文件(代码14).询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的"Singleton"类来处理SQLite连接,并确保整个流程/应用程序具有1个连接实例:

public class DBController {
  private static DBController instance = new DBController();
  private static DBHelper dbHelper;
  public static DBController getInstance()
  {
    return instance;
  }
  public SQLiteDatabase dbOpen(Context context)
  {
    if(dbHelper == null)
        dbHelper = new DBHelper(context);
    return dbHelper.getWritableDatabase();
  }
}

还有DBHelper类本身:

public class DBHelper extends SQLiteOpenHelper {

  public DBHelper(Context context) {
    super(context, "database.db", null, 1);
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
    final String position = "CREATE TABLE test (" +
            "test TEXT NOT NULL);";
    db.execSQL(position);
  }
}

当我经常尝试从数据库中选择"某些信息时,我收到以下错误:

SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) - 
SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) - 
SQLiteLog: (14) statement aborts at 29: [SELECT * FROM test WHERE test='testdata1'] unable to open database file
SQLiteQuery: exception: unable to open database file (code 14); query: SELECT * FROM test WHERE test='testdata1'
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)

我正在运行以下代码来执行查询:

public String getData(Context context)
{
    SQLiteDatabase _db = dbOpen(context);
    Cursor c = _db.rawQuery("SELECT * FROM test WHERE test='testdata1'", null);
    return getDataFromCursor(c).get(0); //gets data from cursor and returns first one
}

我如何管理/改善数据库连接以克服/避免此问题?

在对该材料执行任何查询之前,请

解决方案

(应打开数据库).完成任务后关闭数据库.

private DBHelper dbHelper = new DBHelper(context);

try {
    _db = dbHelper.getWritableDatabase();
} catch (SQLException s) {
    new Exception("Error with DB Open");
}

//然后立即编写查询....然后关闭数据库.

_db.close();

您可以查看我的 git link1 git link2

I have following class "Singleton" to handle SQLite connection and to make sure to have 1 instance of connection for whole process/app:

public class DBController {
  private static DBController instance = new DBController();
  private static DBHelper dbHelper;
  public static DBController getInstance()
  {
    return instance;
  }
  public SQLiteDatabase dbOpen(Context context)
  {
    if(dbHelper == null)
        dbHelper = new DBHelper(context);
    return dbHelper.getWritableDatabase();
  }
}

And DBHelper class itself:

public class DBHelper extends SQLiteOpenHelper {

  public DBHelper(Context context) {
    super(context, "database.db", null, 1);
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
    final String position = "CREATE TABLE test (" +
            "test TEXT NOT NULL);";
    db.execSQL(position);
  }
}

When I frequently try to "SELECT" some info from database I am receiving following error:

SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) - 
SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) - 
SQLiteLog: (14) statement aborts at 29: [SELECT * FROM test WHERE test='testdata1'] unable to open database file
SQLiteQuery: exception: unable to open database file (code 14); query: SELECT * FROM test WHERE test='testdata1'
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)

I am running following code to execute query:

public String getData(Context context)
{
    SQLiteDatabase _db = dbOpen(context);
    Cursor c = _db.rawQuery("SELECT * FROM test WHERE test='testdata1'", null);
    return getDataFromCursor(c).get(0); //gets data from cursor and returns first one
}

How can I manage/improve my database connection to overcome/avoid this problem?

解决方案

before executing any query to this stuff (You should open your database). Close db after completion the task.

private DBHelper dbHelper = new DBHelper(context);

try {
    _db = dbHelper.getWritableDatabase();
} catch (SQLException s) {
    new Exception("Error with DB Open");
}

// Then write your query now.... and then close the db.

_db.close();

You can check out my git link1 git link2

这篇关于SQLite在频繁的“选择"操作中无法打开数据库文件(代码14).询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆