0

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

}

Error Log Cat

2
  • You shouldn't save contexts statically. If you access this code from multiple activities its likely you will get a crash. Commented Mar 15, 2011 at 4:02
  • so how could i use this code from multiple activities. Does it concern with phone read/write access ? Commented Mar 15, 2011 at 4:06

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

my database file size is larger than 1 MB and now i am working on Android 2.1 based, any idea ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.