Programing

프로그래밍 방식으로 Android에서 SQLite 데이터베이스를 삭제하는 방법

lottogame 2020. 6. 12. 22:03
반응형

프로그래밍 방식으로 Android에서 SQLite 데이터베이스를 삭제하는 방법


Android file system프로그래밍 방식으로 데이터베이스 파일을 삭제하고 싶습니까? 셸 스크립트를 실행 adb하여 Android 공간에서 셸 스크립트를 실행하여 데이터베이스를 삭제할 수 있습니까? JUnit테스트 케이스 내에서 ( system()통화 와 함께 ) 이 작업을 수행 할 수 있습니까 ?

Android에서 전체 데이터베이스를 어떻게 삭제합니까? 데이터베이스 생성을 테스트 할 수 있도록 모든 것을 없애야합니다. 나는 테이블을 삭제할 수 있지만 충분하지 않습니다. 이것은 전화가 아닌 에뮬레이터에 있습니다.


컨텍스트가 있고 데이터베이스 이름을 알고 있으면 다음을 사용하십시오.

context.deleteDatabase(DATABASE_NAME);

이 줄이 실행되면 데이터베이스를 삭제해야합니다.


쉘에서 쉽게 입력 할 수 있습니다.

adb shell
cd /data/data
cd <your.application.java.package>
cd databases
su rm <your db name>.db

SQLiteDatabase.deleteDatabase (File file) 정적 메소드가 API 16에 추가되었습니다. 이전 디바이스를 지원하는 앱을 작성하려면 어떻게해야합니까?

나는 시도했다 : file.delete ();

그러나 SQLiteOpenHelper를 망칩니다.

감사.

신경 쓰지 마! 나중에 Context .deleteDatabase ()를 사용하고 있음을 깨달았습니다 . 컨텍스트 하나는 훌륭하게 작동하고 저널도 삭제합니다. 나를 위해 작동합니다.

또한 삭제를 수행하기 전에 SQLiteOpenHelp.close () 를 호출 해야 하므로 LoaderManager를 사용하여 다시 만들 수 있습니다.


시험:

this.deleteDatabase(path); 

또는

context.deleteDatabase(path);

또한 Eclipse에서 DDMS를 사용하면 정말 쉽습니다.

에뮬레이터가 실행 중인지 확인한 다음 Eclipse에서 DDMS Perspective로 전환하십시오. 파일 탐색기에 액세스하여 전체 데이터베이스를 쉽게 삭제하고 삭제할 수 있습니다.


context.deleteDatabase("database_name.db");

이것은 누군가를 도울 수 있습니다. 그렇지 않으면 확장명을 언급해야하며 작동하지 않습니다.


context.deleteDatabase (DATABASE_NAME); 모든 연결이 닫힌 경우에만 데이터베이스를 삭제합니다. 데이터베이스 헬퍼를 처리하기 위해 싱글 톤 인스턴스를 유지 보수하는 경우 열린 연결을 쉽게 닫을 수 있습니다.

데이터베이스 인스턴스를 직접 인스턴스화하여 여러 위치에서 사용하는 경우, 일부 연결이 열려 있어도 deleteDatabase + killProcess가 작업을 수행합니다. 응용 프로그램 시나리오에 앱을 다시 시작하는 데 문제가없는 경우 사용할 수 있습니다.


앱을 제거 할 때 이전 Db를 삭제하십시오.

AndroidManifest.xml 의 애플리케이션 태그에서 android : allowBackup = "false"설정 하면 문제가 해결되었습니다. 이상한 이유로 안드로이드 앱이 앱을 배포 할 때마다 백업에서 복원 된 것 같습니다.


현재 데이터베이스 경로파일 객체를 만든 다음 폴더에서 파일을 삭제할 때 삭제할 수 있습니다

    File data = Environment.getDataDirectory();
    String currentDBPath = "/data/com.example.demo/databases/" + DATABASE_NAME;
    File currentDB = new File(data, currentDBPath);
    boolean deleted = SQLiteDatabase.deleteDatabase(currentDB);

Android 데이터베이스 삭제 방법을 사용했으며 데이터베이스가 성공적으로 제거되었습니다.

public bool DeleteDatabase()
        {
            var dbName = "TenderDb.db";
            var documentDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentDirectoryPath, dbName);
            return Android.Database.Sqlite.SQLiteDatabase.DeleteDatabase(new Java.IO.File(path));
        }

자산의 데이터베이스 구조를 변경 한 후 장치에서 데이터베이스를 "포맷"하기 위해 다음을 사용했습니다. 자산에서 데이터베이스를 다시 읽길 원할 때 MainActivity에서 해당 줄의 주석을 해제하십시오. 그러면 자산 데이터베이스의 사전 데이터베이스와 함께 장치 데이터베이스 값과 구조가 재설정됩니다.

    //database initialization. Uncomment to clear the database
    //deleteDatabase("questions.db");

Next, I will implement a button that will run the deleteDatabase so that the user can reset its progress in the game.


From Application Manager, you can delete whole application with data. Or just data by it self. This includes database.

  1. Navigate to Settings. You can get to the settings menu either in your apps menu or, on most phones, by pulling down the notification drawer and tapping a button there.

  2. Select the Apps submenu. On some phones this menu will have a slightly different name such as Application Manager.

  3. Swipe right to the All apps list. Ignore the lists of Running and Downloaded apps. You want the All apps list.

  4. Select the app you wish to disable. A properties screen appears with a button for Force Stop on the upper left and another for either Disable or Uninstall updates on the upper right side.

  5. Delete data.

참고URL : https://stackoverflow.com/questions/4406067/how-to-delete-sqlite-database-from-android-programmatically

반응형