开发者

Database folder cant be found in android default application storage path to "/data/data/YOUR_PACKAGE/databases/"

Can anybody tell me whats the problem in my code. The dbHelper cant find the database folder in data/data/YOUR_PACKAGE/databases/. When I run my application it says an error occured while doInBackground and the error is ERROR/AndroidRuntime(703): Caused by: java.lang.Error: Problem copying database from resource file. here is the code snippet for my dbHelper class.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
impo开发者_C百科rt android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class SqlHelper extends SQLiteOpenHelper {
public static final String DATABASE_PATH = "/data/data/apppackage-name/databases/";
public static final String DATABASE_NAME = "profiledatabase.db";
private static final int DATABASE_VERSION = 1;

public static final String PROFILES_TABLE = "profiles";
public static final String COLUMN_PROFILE_ID = "profile_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_BIRTHDAY = "birthday";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_GENDER = "gender";

public static final String INTERESTS_TABLE = "interests";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_SELECTED = "selected";

public static final String PROFILESINTERESTS_TABLE = "profiles_interests";
private static long profile_id = -1;

private static final String CREATE_TABLE_1 =
        " create table " + PROFILES_TABLE +
        " (profile_id integer primary key autoincrement," +
        " name text not null, birthday date not null, email text not null, gender text not null);";

private static final String CREATE_TABLE_2 =
        " create table " + INTERESTS_TABLE +
        " (_id integer primary key autoincrement," +
        " title text not null, selected integer);";

private static final String CREATE_TABLE_3 =
        " create table " + PROFILESINTERESTS_TABLE +
        " (profile_id integer primary key," +
        " _id integer);";



public SQLiteDatabase dbSqlite;
private boolean dbExist = false;
private final Context myContext;

public SqlHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

/*@Override
public void onCreate(SQLiteDatabase db) {
    // check if exists and copy database from resource
    createDB();
}*/

@Override
public void onCreate(SQLiteDatabase db) {
createDB();
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
db.execSQL(CREATE_TABLE_3);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("SqlHelper", "Upgrading database from version " + oldVersion
            + " to " + newVersion + ", which will destroy all old data");
    onCreate(db);
}

public void createDatabase() {
    createDB();
}

private void createDB() {
    dbExist = DBExists();
    if (!dbExist) {     
        copyDBFromResource();
    }
}

public boolean dbExisting() {
    return dbExist;
}

private boolean DBExists() {
    SQLiteDatabase db = null;
    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);
    } catch (SQLiteException e) {
        Log.e("SqlHelper", "database not found");
    }
    if (db != null) {
        db.close();
    }
    return db != null ? true : false;
}

private void copyDBFromResource() {
    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;
    try {
        inputStream = myContext.getAssets().open(DATABASE_NAME);
        outStream = new FileOutputStream(dbFilePath);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
    } catch (IOException e) {
        throw new Error("Problem copying database from resource file.");
    }
}

public void openDataBase() throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

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

public void addProfiles( String name, String birthday, String email, String gender) {
      ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("birthday", birthday);
        values.put("email", email);
        values.put("gender", gender);
        profile_id = dbSqlite.insert(PROFILES_TABLE, null, values);
      }

    public void addProfilesInterests( String iid) {
          ContentValues values = new ContentValues();
            values.put(COLUMN_PROFILE_ID, profile_id);
            values.put(COLUMN_ID, iid);
            profile_id = dbSqlite.insert(PROFILESINTERESTS_TABLE, null, values);
          }

    public Cursor getProfiles(){
        return dbSqlite.query(PROFILES_TABLE, new String[] {
                "name", " birthday", "email", "gender"},  null, null, null, null, null);
    }  

    public Cursor getCursor() {
          SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
          queryBuilder.setTables(INTERESTS_TABLE);
          String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_SELECTED };
          Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,null, null, null, "title ASC");
        return mCursor;
}
    public void clearSelections() {
        ContentValues values = new ContentValues();
        values.put("selected", 0);
        this.dbSqlite.update(SqlHelper.INTERESTS_TABLE, values, null, null);
    }

}


contextWrapper.openOrCreateDatabase(sqlDBName, MODE_PRIVATE, null);
sqLiteDatabase = SQLiteDatabase.openDatabase(path, factory, flags);
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, factory);

Use the above method to create SQLite databases.


I figured out the problem. There is actually no database folder inside my /data/data/app-packagename/ folder. The database folder is missing inside the application itself. I tried to create a new emulator and ran the application. But still the database folder is missing. So when my dbHelper class tries to create or copy database from resource it actually cant find the path given in the code. Any clues??????


Are you sure your package name is "apppackage-name"? Or are you replacing this literal somewhere?

From the ERROR message, it appears that your package name is "com.uniqoteq.profile" but I don't see where you are replacing this in the path literal!


The db folder is not created by default for your app its created for the first time when you try to open the databse for the first time. the folder is created and the databse file is also created. when copying the database from resoucre check if folder exist if not create the folder yourself

File f = getDatabasePath(DataBaseAccess.DATABASE_NAME);
String dbPath = f.getAbsolutePath();
createPathIfNotExist(dbPath);
int lastChar = path.lastIndexOf(File.separatorChar);
String dir = path.substring(0, lastChar);
File d = new File(dir);
d.mkdirs();


databases folder does not initially exist in /data/data/YOUR_PACKAGE/.

If you need to do something before you have databases folder, e.g., copying a default DB file if any DB file does not exist, you can simply create databases folder first.

Here is the code for the example:

File dbFile = getApplicationContext().getDatabasePath("any.db");
if (!dbFile.exists()) {
    new File(dbFile.getParent()).mkdirs();
    try {
        InputStream is = getApplicationContext().getAssets().open("any.db");
        OutputStream os = new FileOutputStream(dbFile);

        byte[] buffer = new byte[1024];
        while (is.read(buffer) > 0) {
            os.write(buffer);
        }
        os.flush();

        os.close();
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜