在Android设备中创建SQLite数据库并访问信息

在Android设备中创建SQLite数据库并访问信息,android,database,sqlite,Android,Database,Sqlite,我正在android移动平台上构建一个测验应用程序。 我正在创建一个带有预加载信息的SQLite数据库(quick.SQLite) 启动应用程序时,数据库不是在/data/data/[PROJECT]/databases/quick.sqlite”中创建的。因此,应用程序正在崩溃 正在使用数据库帮助器查找逻辑或示例代码 有人能帮忙吗 -Ranjan这不是一个简单的解决方案,对您来说也不是一个完整的解决方案。我最近自己解决了这个问题,不过备份和恢复Sqlite数据库需要更多的时间 首先,您需要在清

我正在android移动平台上构建一个测验应用程序。 我正在创建一个带有预加载信息的SQLite数据库(quick.SQLite)

启动应用程序时,数据库不是在/data/data/[PROJECT]/databases/quick.sqlite”中创建的。因此,应用程序正在崩溃

正在使用数据库帮助器查找逻辑或示例代码

有人能帮忙吗


-Ranjan

这不是一个简单的解决方案,对您来说也不是一个完整的解决方案。我最近自己解决了这个问题,不过备份和恢复Sqlite数据库需要更多的时间

首先,您需要在清单中设置读写权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


我使用了适合自己需要的c/p代码来导入备份数据库,但我需要设置在实际设备上读取和写入数据的权限。不幸的是,我找不到代码来为原始海报评分。您必须创建一个空数据库才能使用此方法。这将用您的al替换该数据库已准备好创建Sqlite数据库

这只是一个可能的解决方案,希望有人能用一个更简单的解决方案来回应

    String inFileName= Environment.getExternalStorageDirectory() + "/back_up.db"; // This is where the .db file you want to load is stored on your new device
    File dbFile = new File(inFileName);
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(dbFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();

        Toast.makeText(getApplicationContext(), "Exception InFile: "+e.toString(), Toast.LENGTH_LONG).show();// This was to help me define errors

    }
    String outFileName = context.getDatabasePath(DATABASE_NAME).getPath();// This is where Android will look for the database "/data/data/[PROJECT]/databases/quiz.sqlite"
    // Open the empty db as the output stream
    OutputStream output = null;
    try {
        output = new FileOutputStream(outFileName);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Exception OutFile: "+e.toString(), Toast.LENGTH_LONG).show();
    }

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    try {
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Close the streams
    try {
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    helper.close();

我在设计模式下创建了自己的数据库和表。是否需要在dbhelper中创建表?请提供建议。