2011-12-22 147 views
1

嗨我想在我的应用程序中使用预装数据库。但是,当我运行程序的时候,会给出错误SQLiteException:无法打开数据库文件

sqlite3_open_v2("/data/data/com.example.preloaddatabase/databases/ingredients.db", &handle, 2, NULL) failed 

ERROR IN CODE :android.database.sqlite.SQLiteException: unable to open database file 

和我的数据库辅助类:

class IngredientHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_PATH = "/data/data/com.example.preloaddatabase/databases/"; 
    private static final String DATABASE_NAME = "ingredients.db"; 

    private static final String TABLE_NAME = "Ingredients"; 
    private static final String COLUMN_ID = "_id"; 
    private static final String COLUMN_TITLE = "ingredient_name"; 

    private static final int SCHEMA_VERSION = 1; 

    public SQLiteDatabase dbSqlite; 
    private final Context myContext; 

    public IngredientHelper(Context context) { 
     super(context, DATABASE_NAME, null, SCHEMA_VERSION); 
     this.myContext = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("Create Table Ingredients(_id INTEGER PRIMARY KEY,ingredient_name TEXT)"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 

    public void createDatabase() { 
     createDB(); 
    } 

    public void createDB() { 
     boolean dbExist = DbExists(); 
     if (dbExist) { 
      this.getReadableDatabase(); 
      copyDataBase(); 
     } 
    } 

    private boolean DbExists() { 
     SQLiteDatabase db = null; 
     try { 
      String databasePath = DATABASE_PATH + DATABASE_NAME; 
      db = SQLiteDatabase.openDatabase(databasePath, null, 
        SQLiteDatabase.OPEN_READWRITE); 

      db.setLocale(Locale.getDefault()); 
      db.setLockingEnabled(true); 
      db.setVersion(1); 
     } 
     catch (SQLiteException e) { 
      Log.e("SqlHelper", "Database Not Found"); 
     } 
     if (db != null) { 
      db.close(); 
     } 
     return db != null ? true : false; 
    } 

    private void copyDataBase() { 
     InputStream iStream = null; 
     OutputStream oStream = null; 
     String outFilePath = DATABASE_PATH + DATABASE_NAME; 
     try { 
      iStream = myContext.getAssets().open(DATABASE_NAME); 
      oStream = new FileOutputStream(outFilePath); 
      byte[] buffer = new byte[2048]; 
      int length; 
      while ((length = iStream.read(buffer)) > 0) { 
       oStream.write(buffer, 0, length); 
      } 
      oStream.flush(); 
      oStream.close(); 
      iStream.close(); 
     } 
     catch (IOException e) { 
      throw new Error("Problem Copying Database From Resource File"); 
     } 
    } 

    public void openDatabase() throws SQLException { 
     String myPath = DATABASE_PATH + DATABASE_NAME; 
     dbSqlite = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE); 
    } 

    @Override 
    public synchronized void close() { 
     if (dbSqlite != null) { 
      dbSqlite.close(); 
     } 
     super.close(); 
    } 

    public Cursor getCursor() { 
     SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); 
     queryBuilder.setTables(TABLE_NAME); 
     String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE }; 

     Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, 
       null, null, null, null, "ingredient_name ASC"); 
     return mCursor; 
    } 

    public String getName(Cursor c) { 
     return (c.getString(1)); 
    } 
} 

回答

3

第1步:摆脱以下行:

public SQLiteDatabase dbSqlite; 
private final Context myContext; 

步骤2:将getCursor()更改为getReadableDatabase(),而不是引用现在删除的dbSqlite

步骤#3:删除所有涉及dbSqlitemyContext的其他方法。

步骤#4:删除DATABASE_PATH(不要硬编码路径)。

步骤#5:删除createDB(),createDatabase(),DbExists()copyDataBase()方法。如果您想尝试使用您的应用发布数据库,请考虑使用SQLiteAssetHelper

相关问题