简体   繁体   中英

Android sqlite creating database error

i am newbie with android sqlite database. now i just try to create my custom database manager and i got the error at oncreate() function. I already spent 3 days to figure it out but unfortunately i am still stuck in it. 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() . 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. 2. this is very important check that the size of the database is less than 1 mb. Android version previous to 3.0 don't allow file copy of more than 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. 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

hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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