Может кто-нибудь сказать мне, в чем проблема в моем коде.DbHelper не может найти папку базы данных в data/data/YOUR_PACKAGE/databases/
.Когда я запускаю свое приложение, оно говорит, что произошла ошибка во время doInBackground, и ошибка ERROR/AndroidRuntime(703): Caused by: java.lang.Error: Problem copying database from resource file
.Вот фрагмент кода для моего класса dbHelper.
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;
import 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);
}
}