【问题标题】:Android Database creation error.Android 数据库创建错误。
【发布时间】:2011-07-21 08:00:08
【问题描述】:

我已经编写了创建数据库代码;我得到 ERROR/AndroidRuntime(7193): at com.android.xont.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)

这是我的代码:

public class DataBaseHelper extends SQLiteOpenHelper {
  private static String DB_PATH = "/data/data/com.android.xont.controller/databases/";
  private static String DB_NAME = "DEMOUserVentura_HEMA.db";
  private SQLiteDatabase DEMOUserVentura_HEMA;
  private final Context myContext;


    private DataBaseHelper myDbHelper;

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

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 **/
public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase().close();
        //this.getReadableDatabase().getPath();
        //System.out.println( " ===== " +   this.getReadableDatabase().getPath());
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error(e);
        }
    }

}

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;
}

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;
    DEMOUserVentura_HEMA = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {
    if (DEMOUserVentura_HEMA != null)
        DEMOUserVentura_HEMA.close();
        super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

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

当我调用这个活动时,我得到了一个异常。我的数据库大小是:4.55MB。

当我打印正确返回数据库路径的 getReadableDatabase().getPath() 时。

请帮帮我。一个星期以来我一直在拔头发。

当我通过模拟器运行时,它会复制少量文件。不是全部。然后我做了手动复制。

现在问题出在 HTC 手机上。如何修复该异常。请帮帮我。

提前谢谢..

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    AssetManager 具有文件大小限制。在某些设备上,无法打开大于 1MB 的资产文件。您必须将文件拆分成更小的块,将它们放在资产文件夹中,并在应用运行时重新组合这些块。

    请参阅this question,了解如何重新组装这些块。

    要在 OSX 或 Linux 上创建块,您可以使用命令行工具“split”。

    请参阅herehere 或使用带有关键字“android databasehelper assets”的google 来查找有关如何实现DatabaseHelper 的教程,该DatabaseHelper 在db 第一次打开时从asset 文件夹中重新组装块。

    【讨论】:

    • 感谢链接。createNewFile() 方法是什么?你有完整的源代码吗?
    • 如果我们使用android 2.3.3那么我们不需要做chunk吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2014-12-29
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    相关资源
    最近更新 更多