アセットフォルダーに既存のデータベースを1つ追加しています。その後、それらのデータをコピーして新しいデータベースを作成し、ローカルに保存する必要があります。その新しいデータベースに追加することもできます。これらのデータをリストビュー アクティビティにも表示しています。現在、私の活動は停止しています。
私のデータベース活動。
@Override
public void onCreate(SQLiteDatabase db) {
openDataBase();
}
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
//
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
}
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
private void copyDataBase() throws IOException {
String outFileName = DB_PATH + DB_NAME;
OutputStream localDbStream = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead); }
localDbStream.close();
externalDbStream.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (mDb == null) {
createDataBase();
mDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return mDb;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS"+ DATABASE_TABLE_PROJ);
onCreate(db);
}
}
public void saveCategoryRecord(Category category) {
ContentValues values = new ContentValues();
values.put(CATEGORY_COLUMN_TITLE , category.getTitle());
values.put(CATEGORY_COLUMN_CONTENT, category.getContent());
values.put(CATEGORY_COLUMN_DATE, category.getDate());
// Inserting Row
mDb.insert(DATABASE_TABLE_PROJ, null, values);
mDb.close(); // Closing database connection
}
public Cursor fetchProject(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE_PROJ, new String[] {CATEGORY_COLUMN_ID,CATEGORY_COLUMN_TITLE,CATEGORY_COLUMN_CONTENT,CATEGORY_COLUMN_DATE}, CATEGORY_COLUMN_ID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllProjects() {
// TODO Auto-generated method stub
return mDb.query(DATABASE_TABLE_PROJ, new String[] {CATEGORY_COLUMN_ID, CATEGORY_COLUMN_TITLE, CATEGORY_COLUMN_CONTENT, CATEGORY_COLUMN_DATE }, null, null, null, null, null,null);
}
public void close() {
mDbHelper.close();
}
public long createProject(String title, String content, String date) {
ContentValues initialValues = new ContentValues();
initialValues.put(CATEGORY_COLUMN_TITLE , title);
initialValues.put(CATEGORY_COLUMN_CONTENT, content);
initialValues.put(CATEGORY_COLUMN_DATE, date);
return mDb.insert(DATABASE_TABLE_PROJ, null, initialValues);
}
public boolean updateProject(String title, String content, String date) {
ContentValues args = new ContentValues();
args.put(CATEGORY_COLUMN_TITLE, title);
args.put(CATEGORY_COLUMN_CONTENT, content);
args.put(CATEGORY_COLUMN_DATE, date);
return mDb.update(DATABASE_TABLE_PROJ, args, CATEGORY_COLUMN_TITLE + "=" + title, null) > 0;
}
public GinfyDbAdapter(Context ctx) {
this.mCtx = ctx;
String packageName = mCtx.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
try {
externalDbStream = mCtx.getAssets().open(DB_NAME);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public GinfyDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
私のlistactivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item2);
lv =(ListView)findViewById(R.id.list);
mDbHelper = new GinfyDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
私のlogcatエラー
10-04 06:26:47.273: E/AndroidRuntime(1075): FATAL EXCEPTION: main
10-04 06:26:47.273: E/AndroidRuntime(1075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ibetter.Ginfy/com.ibetter.Ginfy.YourPrayerActivity}: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.os.Looper.loop(Looper.java:137)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-04 06:26:47.273: E/AndroidRuntime(1075): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 06:26:47.273: E/AndroidRuntime(1075): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-04 06:26:47.273: E/AndroidRuntime(1075): at dalvik.system.NativeStart.main(Native Method)
10-04 06:26:47.273: E/AndroidRuntime(1075): Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:627)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:313)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:287)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.ibetter.Ginfy.GinfyDbAdapter$DatabaseHelper.openDataBase(GinfyDbAdapter.java:193)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.ibetter.Ginfy.GinfyDbAdapter$DatabaseHelper.onCreate(GinfyDbAdapter.java:101)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.ibetter.Ginfy.GinfyDbAdapter.open(GinfyDbAdapter.java:276)
10-04 06:26:47.273: E/AndroidRuntime(1075): at com.ibetter.Ginfy.YourPrayerActivity.onCreate(YourPrayerActivity.java:118)
10-04 06:26:47.273: E/AndroidRuntime(1075): at android.app.Activity.performCreate(Activity.java:5104)
アクティビティが停止しました。Google で検索すると、open 関数でエラーが表示されます。