데이터베이스를 Android Studio로 가져 오려고합니다. 지금은 자산 폴더에 데이터베이스가 있으며이를 목록보기에 표시하고 있습니다. "파일이 암호화되었거나 데이터베이스가 아닙니다."오류가 발생합니다."파일이 암호화되었거나 데이터베이스가 아닙니다"오류
Android Studio에서 지원하는 데이터베이스 파일 형식이 확실하지 않습니다. SQL을 사용하고 있습니다. 데이터베이스 헬퍼 클래스 Ingredient Helper에 이름을 지정했습니다.
나는 당신이 제안 할 수있는 제안을 정말로 기뻐할 것입니다. 여기에 내 logcat 및 java 파일이 있습니다.
감사합니다!
로그 캣 :
06-15 21:04:52.504 2157-2157/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
06-15 21:04:52.504 2157-2157/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-15 21:04:52.787 3023-3023/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
06-15 21:04:52.788 3023-3023/? E/android.os.Debug﹕ failed to load memtrack module: -2
06-15 21:04:52.940 3054-3054/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
06-15 21:04:52.940 3054-3054/? E/SQLiteLog﹕ (14) os_unix.c:31278: (2) open(/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql) -
06-15 21:04:52.941 3054-3054/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.dsallee.elsdbexecute.IngredientHelper.DBExists(IngredientHelper.java:69)
at com.dsallee.elsdbexecute.IngredientHelper.createDB(IngredientHelper.java:53)
at com.dsallee.elsdbexecute.IngredientHelper.createDatabase(IngredientHelper.java:48)
at com.dsallee.elsdbexecute.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-15 21:04:52.941 3054-3054/? E/SqlHelper﹕ database not found
06-15 21:04:58.360 3054-3054/? E/SQLiteLog﹕ (26) file is encrypted or is not a database
06-15 21:04:58.360 3054-3054/? E/DefaultDatabaseErrorHandler﹕ Corruption reported by sqlite on database: /data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql
06-15 21:04:58.360 3054-3054/? E/DefaultDatabaseErrorHandler﹕ deleting the database file: /data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql
06-15 21:04:58.360 3054-3054/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
06-15 21:04:58.360 3054-3054/? E/SQLiteLog﹕ (14) os_unix.c:31278: (2) open(/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql) -
06-15 21:04:58.360 3054-3054/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.dsallee.elsdbexecute/databases/sqlite3.sql'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:794)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.dsallee.elsdbexecute.IngredientHelper.openDatabase(IngredientHelper.java:121)
at com.dsallee.elsdbexecute.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-15 21:04:58.360 3054-3054/? E/Error﹕ Error in codeandroid.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
주요 활동
public class MainActivity extends Activity {
private IngredientHelper dbIngredientHelper = null;
private Cursor ourCursor = null;
private IngredientAdapter adapter = null;
private CursorAdapter CursorAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.myListView);
dbIngredientHelper = new IngredientHelper(this); // initializes helper
dbIngredientHelper.createDatabase();
dbIngredientHelper.openDatabase();
ourCursor = dbIngredientHelper.getCursor();
startManagingCursor(ourCursor);
adapter = new IngredientAdapter(ourCursor);
myListView.setAdapter(adapter);
} catch (Exception e) {
Log.e("Error", "Error in code" + e.toString());
e.printStackTrace();
}
}
class IngredientAdapter extends CursorAdapter {
IngredientAdapter(Cursor c) {
super(MainActivity.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
IngredientHolder holder = (IngredientHolder) row.getTag();
holder.populateFrom(c, dbIngredientHelper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
IngredientHolder holder = new IngredientHolder(row);
row.setTag(holder);
return (row);
}
}
static class IngredientHolder {
private TextView note = null;
IngredientHolder(View row) {
note = (TextView)row.findViewById(R.id.noteText);
}
void populateFrom(Cursor c, IngredientHelper r) {
note.setText(r.getName(c));
}
}
}
성분 도우미
class IngredientHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.dsallee.elsdbexecute/databases/";
private static final String DATABASE_NAME = "redo.sql";
private static final int SCHEMA_VERSION = 1;
public static final String TABLE_NAME = "ELS";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "number";
public SQLiteDatabase dbSqlite;
private final Context myContext;
public IngredientHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist = DBExists();
if (!dbExist) {
this.getReadableDatabase();
copyDBFromResource();
}
}
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 Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[]{COLUMN_ID, COLUMN_TITLE};
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
null, null, null, null);
// ACTUAL TABLE QUERY ABOVE
// COLUMNS TO RETURN IS COLUMNS TO BE DISPLAYED IN THE LISTVIEW
return mCursor;
}
public String getName(Cursor c) {
return (c.getString(1));
// GET COLUMN INDEX 1 FROM ASCOLUMNSTORETURN
}
}
실제로 자산 폴더 ([ref] (http://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder))에서 직접 DB를 열 수 없습니다. 먼저'copyDBFromResource()'메소드를 시도해야하지만, 여전히 그 전에 자신의 데이터베이스를 생성해야한다. SQLiteAssetHelper를 사용하면이 프로세스가 훨씬 쉬워 지므로 사용하는 것이 좋습니다. –