Android sqlite创建数据库错误

时间:2011-03-15 03:49:43

标签: java android database sqlite

我是android sqlite数据库的新手。现在我只是尝试创建我的自定义数据库管理器,我在oncreate()函数中得到错误。我已经花了3天时间搞清楚,但不幸的是我仍然陷入其中。任何好主意都会受到赞赏。

public class DBManager extends SQLiteOpenHelper {
private SQLiteDatabase db;
private static Context myContext;
public static final String DB_NAME = "goldenland.db";
public static final String TB_CAT = "tbl_categories";
public static final String TB_ARTICLE = "tbl_articles";
public static final String TB_SUBCAT = "tbl_subcategories";
public static final String TB_SCHEDULE = "tbl_schedule";
public static final String TB_CONTENT = "tbl_contents";
public static final String TB_CITY = "tbl_cities";
public static final String name = "name";
public static String DB_PATH = "/data/data/com.gokiri.goldenland/databases/";

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

public void createDataBase() throws IOException {
    boolean dbExit = checkDataBase();

    if (dbExit) {
        System.out.println("Testing");
    } else {
        this.getReadableDatabase();

        try {
            copyDataBase();

        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());

        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDb = null;

    try {
        String myPath = DB_PATH + DB_NAME;

        checkDb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLException e) {

    }

    if (checkDb != null) {
        checkDb.close();
    }
    return checkDb != null ? true : false;
}

public void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open("goldenland_2.sqlite");

    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDatabase() throws SQLiteException {

    String myPath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

Error Log Cat

3 个答案:

答案 0 :(得分:1)

有些事可能是错的。您的数据库确实存在于资源/资产中吗?你在写正确的目录吗?逐步调试调试器将对诊断问题大有帮助。

您可以通过copyDatabase获取String参数this.getReadableDatabase().getPath()进行健全性检查。您甚至可以尝试将其写入日志,以查看您是否正在写入正确的数据库目录。

答案 1 :(得分:1)

很少要检查。 1.在方法checkDataBase中以OPEN_READONLY模式打开数据库。 2.这是非常重要的检查,数据库的大小小于1 MB。 3.0之前的Android版本不允许超过1 MB的文件副本。

<强>更新 您需要将数据库文件拆分为1Mb的部分,逐个复制所有部分并再次将它们连接在一起。请参阅以下link

答案 2 :(得分:0)

除非你没有提出所有代码,否则在我看来你错过了一些东西。我使用本教程并没有问题

http://www.devx.com/wireless/Article/40842/1954

希望有所帮助

相关问题