私はpalavras.dbという名前のこのデータベースがあります。内部データベースフォルダエラーに資産フォルダから既存のデータベースをコピーする
:
は、それが私ののresフォルダの内部に配置されています
そして、このようなデータベースを使用します:
public class UserListActivity extends AppCompatActivity {
/* As soon as this activity starts, it sets it's recycler view's adapter as
list of words from a result from a query */
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
Database db = new Database(getApplicationContext());
List<Word> words = db.search();
mAdapter = new WordAdapter(words);
mRecyclerView.setAdapter(mAdapter);
}
Databaseクラス:
public class Database {
private SQLiteDatabase db;
private String tableName = "'PALAVRAS'";
private DatabaseHelper databaseHelper;
public Database(Context context) {
databaseHelper = new DatabaseHelper(context);
databaseHelper.createDatabase();
databaseHelper.openDatabase();
}
public List<Word> search() {
return databaseHelper.search(tableName);
}
データベースのヘルパークラス:
public class DatabaseHelper extends SQLiteOpenHelper {
private String DB_PATH = "";
private static final String DB_NAME = "palavras.db";
private SQLiteDatabase mDatabase;
private final Context mContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.mContext = context;
this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
Log.d("porra", DB_PATH);
}
public void createDatabase() {
boolean dbExists = checkDatabase();
if(dbExists) {
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
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;
mDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public synchronized void close() {
if (mDatabase != null) {
mDatabase.close();
}
super.close();
}
public List<Word> search(String tableName) {
List<Word> list = new ArrayList<Word>();
String[] colunas = new String[]{"original", "traduzido"};
Cursor c = mDatabase.rawQuery("SELECT * FROM "+tableName, null);
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
Log.d("Porra", "Table Name=> "+c.getString(0));
c.moveToNext();
}
}
/*
Cursor cursor = db.query(tableName, colunas, null, null, null, null, "original ASC");
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
Word w = new Word();
w.setOriginal(cursor.getString(0));
w.setTraduzido(cursor.getString(1));
list.add(w);
} while(cursor.moveToNext());
}
*/
// dbCore.close();
return list;
}
そして、私はここでそれを実行し、すべて私が得るものです:
08-01 19:20:45.723 4453-4453/app.visage.testesql E/SQLiteLog: (14) cannot open file at line 32456 of [bda77dda96] 08-01 19:20:45.723 4453-4453/app.visage.testesql E/SQLiteLog: (14) os_unix.c:32456: (2) open(/data/data/app.visage.testesql/databases/palavras.db) - 08-01 19:20:45.725 4453-4453/app.visage.testesql E/SQLiteDatabase: Failed to open database '/data/data/app.visage.testesql/databases/palavras.db'. 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:209) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 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:808) at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671) at app.visage.testesql.database.DatabaseHelper.checkDatabase(DatabaseHelper.java:53) at app.visage.testesql.database.DatabaseHelper.createDatabase(DatabaseHelper.java:36) at app.visage.testesql.database.Database.(Database.java:27) at app.visage.testesql.activities.UserListActivity.onCreate(UserListActivity.java:32) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 08-01 19:20:45.760 4453-4453/app.visage.testesql D/AndroidRuntime: Shutting down VM 08-01 19:20:45.761 4453-4453/app.visage.testesql E/AndroidRuntime: FATAL EXCEPTION: main Process: app.visage.testesql, PID: 4453 java.lang.Error: Error copying database at app.visage.testesql.database.DatabaseHelper.createDatabase(DatabaseHelper.java:44) at app.visage.testesql.database.Database.(Database.java:27) at app.visage.testesql.activities.UserListActivity.onCreate(UserListActivity.java:32) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
は、データベースのコピーに失敗しましたし、それを開くことができませんでした。
私のコードには何か問題はありますか?
また、データベースフォルダを参照すると、そこにコピーされたことがわかります!全部ではなく、表PALAVRASは存在しませんでした。android_metadata。