将预先填充的Sqlite DB复制到Android App数据库文件夹中

问题描述 投票:0回答:1

我使用预填充的SQLite数据库为Android开发了一个大型程序,它运行良好。现在,我正在尝试重用它,但它在Android 6.0+设备上不起作用,在较旧的android版本上,它仍在工作。问题是每次我尝试访问数据时,数据库都是空的。我正在尝试一些东西,并且制作了我能想到的最简单的应用程序。

我一直在尝试通过任何方法更改“ /data/data/..../database”以获得正确的位置,但是无法使其正常工作。也许我需要特殊的技巧才能使其在6.0+ Android之类的系统中工作?有什么帮助吗?谢谢!

public class MainActivity extends AppCompatActivity {

    private DataBaseHelper dbHelper;

    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1 = findViewById(R.id.textViewText);

        databaseConexion();

        textView1.setText(dbHelper.getAll());
    }


    private void databaseConexion()
    {
        dbHelper = new DataBaseHelper(this);
        try {
            dbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");

        }
    }
}
public class DataBaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/enkend.apps.proyects.pogo.sqlitetest/databases/";

    private static String DB_NAME = "Test.db";

    private SQLiteDatabase myDataBase;

    private final Context myContext;


    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     *
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {

        myContext.deleteDatabase(DB_NAME);
        boolean dbExist = checkDataBase();

        if (!dbExist) {

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }
    }

    private boolean checkDataBase() {

        File databasePath = myContext.getDatabasePath(DB_NAME);
        return databasePath.exists();
    }


    private void copyDataBase() throws IOException {


        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        //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();

    }

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    public String getAll()
    {
       String str= "";

        // Select All Query
        String selectQuery = "SELECT Name FROM Fortnight ORDER BY Name";

        SQLiteDatabase db = this.getReadableDatabase();
        try {

            Cursor cursor = db.rawQuery(selectQuery, null);
            try {

                // looping through all rows and adding to list
                if (cursor.moveToFirst()) {
                    //int i = 0;
                    do {
                        return cursor.getString(0);
                    } while (cursor.moveToNext());

                }

            } finally {
                try { cursor.close(); } catch (Exception ignore) {}
            }

        } finally {
            try { db.close(); } catch (Exception ignore) {}
        }
        return str;
    }
android database sqlite android-6.0-marshmallow
1个回答
0
投票

您正在使用this.getReadableDatabase();创建一个新的空数据库,并且然后试图用copyDataBase();覆盖它吗?我认为应该是相反的方式。

© www.soinside.com 2019 - 2024. All rights reserved.