简体   繁体   English

Android sqlite创建数据库错误

[英]Android sqlite creating database error

i am newbie with android sqlite database. 我是android sqlite数据库的新手。 now i just try to create my custom database manager and i got the error at oncreate() function. 现在我只是尝试创建我的自定义数据库管理器,我在oncreate()函数中得到错误。 I already spent 3 days to figure it out but unfortunately i am still stuck in it. 我已经花了3天时间搞清楚,但不幸的是我仍然陷入其中。 Any brilliant idea would be appreciate. 任何好主意都会受到赞赏。

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

}

错误日志Cat

A few things might be wrong. 一些事情可能是错的。 Does your database really exist in res/assets? 您的数据库确实存在于资源/资产中吗? Are you writing to the correct directory? 你在写正确的目录吗? Stepping through with the debugger would go a long way toward diagnosing the problem. 逐步调试调试器将大大有助于诊断问题。

You might get a sanity check by having copyDatabase take a String argument, which would be this.getReadableDatabase().getPath() . 您可以通过让copyDatabase获取String参数(可能是this.getReadableDatabase().getPath()来进行健全性检查。 You might even try writing that out to the log to see if you're writing to the correct database directory. 您甚至可以尝试将其写入日志以查看是否正在写入正确的数据库目录。

Few things to check. 几件事情无法检查。 1. in method checkDataBase open the database in OPEN_READONLY mode. 1.在方法checkDataBaseOPEN_READONLY模式打开数据库。 2. this is very important check that the size of the database is less than 1 mb. 2.这是非常重要的检查,数据库的大小小于1 MB。 Android version previous to 3.0 don't allow file copy of more than 1 mb. 3.0之前的Android版本不允许超过1 MB的文件副本。

Update: You will need to split the database file into parts of 1Mb each, copy all parts one by one and join them together again. 更新:您需要将数据库文件拆分为1Mb的部分,逐个复制所有部分并再次将它们连接在一起。 refer to following link 请参阅以下链接

unless you didnt put up all your code, it seems to me that you are missing somethings. 除非你没有提出所有代码,否则在我看来你错过了一些事情。 I used this tutorial and had no problems 我使用本教程并没有问题

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

hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM