データベースをAndroidスタジオにインポートしようとしています。 今、資産フォルダにデータベースがあり、それをリストビューに表示しています。 「ファイルが暗号化されているか、データベースではありません」というエラーが表示されます。"ファイルが暗号化されているかデータベースではありません"エラー
Android Studioがサポートしているデータベースファイルの種類がわかりません。私はSQLを使用しています。 データベースHelperクラスIngredient Helperという名前を付けました。
私はあなたが提供できる提案を感謝します。ここに私のlogcatとjavaファイルがあります。
ありがとうございました!
Logcat:
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
}
}
実際にはassetsフォルダ([ref](http://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder))から直接DBを開くことはできません。まず 'copyDBFromResource()'メソッドを試す必要がありますが、それ以前に独自のデータベースを作成する必要があります。 SQLiteAssetHelperを使用することを強くお勧めします。 –