0

我正在开发 android 应用程序。我的客户端为我提供了一个很大的数据库。它几乎是 2.3Mb。最初我不知道如何使用现有数据库,我遵循了本教程 教程

用于访问现有数据库。但我得到了错误。我想知道如何访问这种类型的大型数据库。谁能帮我。

在这里,我有我的 DataBaseHelper 类。

公共类 DataBaseHelper 扩展 SQLiteOpenHelper {

// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.readdatabase/databases/";

private static String DB_NAME = "categories.sqlite";

private SQLiteDatabase myDataBase;

private final Context myContext;

public DataBaseHelper(Context context) {

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

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
    } else {

        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {

        // database does't exist yet.

    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    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();

}

public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);

}

@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 Cursor performTransaction(String query) {

    Cursor cur = myDataBase.rawQuery(query, null);

    return cur;
}

}

这是我的主要课程

公共类 MainActivity 扩展 Activity {

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

    DataBaseHelper db = new DataBaseHelper(this);
    try {
        db.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
    db.openDataBase();

    String query = "select * from PrayerInfo";
    Cursor cursor = db.performTransaction(query);
    System.out.println(cursor.getColumnCount());
}

}

它在 Android 2.3 和更高版本中运行良好。但它在较低版本上不起作用。我收到错误之类的错误。任何人都可以帮助我

java.lang.Error:在 com.example.readdatabase.MainActivity.onCreate(MainActivity.java:18) 的 com.example.readdatabase.DataBaseHelper.createDataBase(DataBaseHelper.java:48) 复制数据库时出错

4

2 回答 2

0

这是通常的做法。您可以将现有数据库复制到资产文件夹,然后在设备上安装后,用它填充设备的内部数据库。错误是什么?如果您不发布您的 LogCat 和相关的 Java 代码,那么没有人可以帮助您...

于 2013-02-05T13:49:38.560 回答
0

这个库可能对你有用:Android SQLite Asset Helper

请注意,您将无法直接使用原始数据库,只能使用副本,因此会浪费一些空间。


另一种解决方案可能是将原始数据库转换为 SQL(可以压缩为 zip 文件),并在SqliteOpenHelper.onCreate方法中播放脚本。

请参阅此链接了解如何执行此操作:如何转储某些 SQLite3 表的数据?

于 2013-02-05T13:59:16.807 回答