4

我在 android 应用程序中有以下代码作为 DataBaseHelper:

    public class DbUtils {

    private static final String DB_PATH = "/data/data/com.project.skcompanion/databases/";

    private static final String DB_NAME = "basesh.sqlite";



    public static void createDatabaseIfNotExists(Context context) throws IOException {
        Log.d("check1", "check1");
        boolean createDb = false;

        File dbDir = new File(DB_PATH);

        File dbFile = new File(DB_PATH + DB_NAME);

        if (!dbDir.exists()) {
            dbDir.mkdir();
            createDb = true;
        }
        else if (!dbFile.exists()) {
            createDb = true;
        }
        else {
            // Check that we have the latest version of the db
            boolean doUpgrade = false;

            if (doUpgrade) {
                dbFile.delete();
                createDb = true;
            }
        }
        Log.d("check2", "check2");
        if (createDb) {
            Log.d("check3", "check3");
            // Open your local db
            AssetManager am = context.getAssets();
            InputStream myInput = am.open(DB_NAME);
            Log.d("check4", "check4");
            // Open the empty db
            OutputStream myOutput = new FileOutputStream(dbFile);
            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;

            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }

    }



    public static SQLiteDatabase getStaticDb() {

        return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

    }

但是当代码尝试打开 basesh.sqlite 时,它​​会发送 FileNotFoundException。我在资产/数据库中有我的 basesh.sqlite 文件。经过一些调试,我认为问题出在这一行。

InputStream myInput = am.open(DB_NAME);

但我不知道为什么它没有打开我的文件。提前致谢。

4

3 回答 3

0

您不需要创建文件本身。只需创建一个数据库,就会创建。

试试这个助手:

public class DatabaseHelper extends SQLiteOpenHelper {

    private final Context context;

    public DatabaseHelper(Context context) {
        super(context, context.getString(R.string.app_name), null, Integer.valueOf(context.getString(R.string.database_version)));
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createDb(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String[] versions = context.getResources().getStringArray(R.array.database_upgrade);
        for (int i = oldVersion; i < newVersion; i++) {
            String[] statements = versions[i - 1].split(";");
            for (String statement : statements) {
                db.execSQL(statement);
            }
        }
    }

    private void createDb(SQLiteDatabase db) {
        try {
            db.beginTransaction();
            String[] tables = context.getResources().getStringArray(R.array.database_tables);
            for (String table : tables) {
                db.execSQL(table);
            }
        } catch (SQLException e) {
            Log.e(context.getString(R.string.app_name), context.getString(R.string.database_access_error));
        } finally {
            db.endTransaction();
        }
    }
}

并且,对于资源,包括更新其结构的数据库的新版本号:

<string name="database_version">2</string>

<string-array name="database_tables">
    <item>CREATE TABLE "COLLECT" (
        _ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
        DATE TEXT NOT NULL,
        SENT INTEGER, 
        OM REAL,
        CMSR REAL,
        DC REAL
        );</item>
</string-array>
<string-array name="database_upgrade">
    <item>ALTER TABLE COLLECT ADD COLUMN SENT INTEGER;</item>
</string-array>

如您所见,当database_version是 1 时,sqliteHelper 只是创建了我的collect表,作为我的示例。如果数据库已经存在,例如示例,并且版本号现在是 2 或更高,sqliteHelper 会更新 db 结构读取数组database_upgrade迭代其所有项目。

于 2013-03-28T02:25:31.587 回答
0

您的问题是您将数据库文件 basesh.sqlite 放入assets/databases,这是错误的。context.getAssets().open("basesh.sqlite") 进行调查assets,但什么也没找到。所以你需要把它放在下面assets

于 2013-03-28T02:04:52.990 回答
0

你正在尝试打开

InputStream myInput = am.open(DB_NAME);

当您创建数据库时,您在此路径上创建它

private static final String DB_PATH = "/data/data/com.project.skcompanion/databases/";

将您的代码更新为此

InputStream myInput = am.open(DB_PATH+DB_NAME);
于 2013-03-28T02:09:09.150 回答