额外的数据库正被添加到Android文件资源管理器中

时间:2014-02-22 03:51:18

标签: android sqlite

我有一个现有的数据库,我已经能够复制到我的文件资源管理器,继续Youtube和tweek代码,但我得到3个数据库。在/data/data/com.xxx.xxx/databases下我得到以下内容:

ab.sqlite - 这是我想要的 ab.sqlite-journal - 我认为这是一个需要创建的Android文件

现在在/data/data/com.xxx.xxx下我得到以下内容:

databasesab.sqlite - 来自哪里?

这是DataBaseHelper代码:

`private static final String DB_PATH ="/data/data/com.xxx.xxx/databases";
    private static final String DB_NAME ="ab.sqlite";
    private static final int DB_VERSION=1;

//inserting Table names and column names

public static final String TABLE_NAME = "abc";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_1 = "Voltage";


public SQLiteDatabase dbSqlite;

private final Context myContext;

public DatabaseHelper (Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}

@Override
public void onCreate (SQLiteDatabase db) {

}

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

}
public void createDatabase() {
    createDB();
}
private void createDB() {
    boolean dbExist = DBExists();

    if (!dbExist) {

        //Method we creates an empty database to default system location
        //Overwrite the database with our database.
        this.getReadableDatabase();

        //Copy the database we included
        copyDBFromResource();
    }   
}

    private boolean DBExists() {

        SQLiteDatabase db = null;

        try {
            String databasePath = DB_PATH + DB_NAME;
            db = SQLiteDatabase.openDatabase (databasePath, null, SQLiteDatabase.OPEN_READWRITE);
            db.setLocale (Locale.getDefault());
            db.setVersion(1);

        }catch (SQLiteException e) {

            Log.e("SqlHelper", "database not found");

        }
        if (db != null) {

            db.close();

        }

        return db !=null ? true : false;

}
    private void copyDBFromResource() {

        InputStream inputStream = null;
        OutputStream outStream = null;
        String dbFilePath = DB_PATH + DB_NAME;

        try {

            inputStream = myContext.getAssets().open(DB_NAME);

            outStream = new FileOutputStream(dbFilePath);

            byte [] buffer = new byte [1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
            }

            outStream.flush();
            outStream.close();
            inputStream.close();

        } catch (IOException e) {

            throw new Error ("Problem copying database from resource file.");

        }

        }   
}
`        

任何帮助都将受到赞赏,因为我似乎无法弄清楚这个问题。

2 个答案:

答案 0 :(得分:0)

问题在于:

private static final String DB_PATH ="/data/data/com.xxx.xxx/databases";
private static final String DB_NAME ="ab.sqlite";

在这里:

String databasePath = DB_PATH + DB_NAME;

你在数据库之后缺少斜杠。

答案 1 :(得分:0)

问题出在这里

String dbFilePath = DB_PATH + DB_NAME;

这就是为什么你要获得databasesab.sqlite,数据库+ ab