简体   繁体   English

SQLiteException:无法打开数据库文件

[英]SQLiteException: unable to open database file

Hi I am trying to use preloaded database in my app. 嗨,我想在我的应用程序中使用预加载的数据库 but when I am run the app it will give the error 但是当我运行应用程序时,它会给出错误

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

And My Database Helper Class is: 我的数据库助手类是:

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));
    }
}

Step #1: Get rid of the following lines: 第一步:摆脱以下几行:

public SQLiteDatabase dbSqlite;
private final Context myContext;

Step #2: Change getCursor() to call getReadableDatabase() instead of referencing the now-removed dbSqlite . 步骤2:更改getCursor()以调用getReadableDatabase()而不是引用现在删除的dbSqlite

Step #3: Delete all other methods that refer to dbSqlite or myContext . 步骤#3:删除引用dbSqlitemyContext所有其他方法。

Step #4: Delete DATABASE_PATH (do not hardcode paths). 步骤#4:删除DATABASE_PATH (不要硬编码路径)。

Step #5: Delete the createDB() , createDatabase() , DbExists() , and copyDataBase() methods. 步骤#5:删除createDB()createDatabase()DbExists()copyDataBase()方法。 If you wish to try to ship a database with your app, consider using SQLiteAssetHelper . 如果您希望尝试使用您的应用程序发布数据库,请考虑使用SQLiteAssetHelper

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新后:“ SQLiteException:无法打开数据库文件” - after update: “SQLiteException: unable to open database file” Android android.database.sqlite.SQLiteException:无法打开数据库文件 - Android android.database.sqlite.SQLiteException: unable to open database file 从不同的包访问数据库:它给出错误:android.database.sqlite.SQLiteException:无法打开数据库文件 - Accessing database from different package : It give error : android.database.sqlite.SQLiteException: unable to open database file 随机异常android.database.sqlite.SQLiteException:无法打开数据库文件 - Random exception android.database.sqlite.SQLiteException: unable to open database file 首次将 .apk 文件上传到市场后出现“SQLiteException:无法打开数据库文件”(Android 2.3.5) - "SQLiteException: unable to open database file" after first uploading the .apk file to the market (Android 2.3.5) 无法打开数据库Sqlite文件 - unable to open database Sqlite file 无法打开sqlite数据库文件 - Unable to open sqlite database file SQLcipher SQLiteException: 错误代码 14: 无法打开数据库 - SQLcipher SQLiteException: error code 14: Could not open database 无法启动活动:android.database.sqlite.SQLiteException - Unable to start activity: android.database.sqlite.SQLiteException net.sqlcipher.database.SQLiteException:文件已加密或不是数据库 - net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM