Programing

장치에서 sqlite 데이터베이스의 위치

crosscheck 2020. 8. 23. 08:21
반응형

장치에서 sqlite 데이터베이스의 위치


확장 SQLiteOpenHelper및 재정 의 기본 방법을 사용하여 프로그래밍 방식으로 sqlite 데이터베이스를 만들었습니다 onCreate(). 이런 식으로 db는 필요할 때 즉시 생성됩니다.

sqlite 브라우저를 사용하여 OS X 컴퓨터에서 db 파일의 내용을 확인하고 싶습니다. db 파일의 이름을 알고 있지만 장치에서 찾을 수 없습니다. USB를 통해 장치에 연결하고 파인더와 터미널을 확인했지만 db 파일을 찾을 수 없습니다.

Android 장치에서 sqlite 데이터베이스의 기본 위치는 무엇입니까?


생성 된 데이터베이스를 찾을 수 있습니다. <your-database-name>

//data/data/<Your-Application-Package-Name>/databases/<your-database-name>

파일 탐색기를 사용하여 꺼내고 SQLiteExplorer에서 사용하려면 .db3 확장자를 갖도록 이름을 바꿉니다.

DDMS의 파일 탐색기를 사용하여 에뮬레이터 디렉터리로 이동합니다.


이를 위해 내가 한 일은

File f=new File("/data/data/your.app.package/databases/your_db.db3");
FileInputStream fis=null;
FileOutputStream fos=null;

try
{
  fis=new FileInputStream(f);
  fos=new FileOutputStream("/mnt/sdcard/db_dump.db");
  while(true)
  {
    int i=fis.read();
    if(i!=-1)
    {fos.write(i);}
    else
    {break;}
  }
  fos.flush();
  Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
  e.printStackTrace();
  Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
}
finally
{
  try
  {
    fos.close();
    fis.close();
  }
  catch(IOException ioe)
  {}
}

이렇게하려면 앱에 SD 카드에 액세스 할 수있는 권한이 있어야하며 manifest.xml에 다음 설정을 추가해야합니다.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

훌륭한 방법은 아니지만 작동합니다.


컨텍스트에는 많은 경로 함수가 포함되어 있습니다. Context.getXXXPath ()
그중 하나는 dbname이라는 데이터베이스의 절대 경로를 반환하는 android.content.Context.getDatabasePath (String dbname)입니다.

Context ctx = this; // for Activity, or Service. Otherwise simply get the context.
String dbname = "mydb.db";
Path dbpath = ctx.getDatabasePath(dbname);

이 경우 반환 된 경로는 다음과 같습니다.

/data/data/com.me.myapp/databases/mydb.db

이 경로는 SQLiteOpenHelper를 사용하여 DB를 여는 경우 자동 생성됩니다.


실제 장치에 대해 이야기하는 경우 /data/data/<application-package-name>액세스 할 수 없습니다. 루트 권한이 있어야합니다 ...


이것은 오래된 질문이지만 대답하면 다른 사람들에게 도움이 될 수 있습니다.

Android가 데이터베이스를 저장하는 기본 경로는 루팅되지 않은 기기에서 액세스 할 수 없습니다. 따라서 데이터베이스 파일에 액세스하는 가장 쉬운 방법 (디버깅 환경에만 해당)은 클래스의 생성자를 수정하는 것입니다.

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "/mnt/sdcard/database_name.db", null, 0);
    }
}

Remember to change for production environments with these lines:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "database_name.db", null, 0);
    }
}

/data/data/packagename/databases/

ie

/data/data/com.example.program/databases/


A SQLite database is just a file. You can take that file, move it around, and even copy it to another system (for example, from your phone to your workstation), and it will work fine. Android stores the file in the /data/data/packagename/databases/ directory. You can use the adb command or the File Explorer view in Eclipse (Window > Show View > Other... > Android > File Explorer) to view, move, or delete it.


well this might be late but it will help. You can access the database without rooting your device through adb

start the adb using cmd and type the following commands

-run-as com.your.package  
-shell@android:/data/data/com.your.package $ ls  
cache  
databases  
lib  
shared_prefs  

Now you can open from here on.


If you name your db as a file without giving a path then most common way to get its folder is like:

final File dbFile = new File(getFilesDir().getParent()+"/databases/"+DBAdapter.DATABASE_NAME);

where DBAdapter.DATABASE_NAME is just a String like "mydatabase.db".
Context.getFilesDir() returns path for app's files like: /data/data/<your.app.packagename>/files/ thats why you need to .getParent()+"/databases/", to remove "files" and add "databases" instead.
BTW Eclipse will warn you about hardcoded "data/data/" string but not in this case.


By Default it stores to:    

String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;

Where:

String DATABASE_NAME = "your_dbname";
String PACKAGE_NAME = "com.example.your_app_name";

And check whether your database is stored to Device Storage. If So, You have to use permission in Manifest.xml :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You can access it using adb shell how-to

Content from above link:

Tutorial : How to access a Android database by using a command line. When your start dealing with a database in your program, it is really important and useful to be able to access it directly, outside your program, to check what the program has just done, and to debug.

And it is important also on Android.

Here is how to do that :

1) Launch the emulator (or connect your real device to your PC ). I usually launch one of my program from Eclipse for this. 2) Launch a command prompt in the android tools directory. 3) type adb shell. This will launch an unix shell on your emulator / connected device. 4) go to the directory where your database is : cd data/data here you have the list of all the applications on your device Go in your application directory ( beware, Unix is case sensitive !! ) cd com.alocaly.LetterGame and descend in your databases directory : cd databases Here you can find all your databases. In my case, there is only one ( now ) : SCORE_DB 5) Launch sqlite on the database you want to check / change : sqlite3 SCORE_DB From here, you can check what tables are present : .tables 6) enter any SQL instruction you want : select * from Score;

This is quite simple, but every time I need it, I don't know where to find it.


If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.

The parts of the above directory are constructed based on the following rules. DATA is the path which the Environment.getDataDirectory() method returns. APP_NAME is your application name. FILENAME is the name you specify in your application code for the database.


You can also check whether your IDE has a utility like Eclipse's DDMS perspective which allows you to browse through the directory and/or copy files to and from the Emulator or a rooted device.


Define your database name like :

private static final String DATABASE_NAME = "/mnt/sdcard/hri_database.db";

And you can see your database in :

storage/sdcard0/yourdatabasename.db

public class MySQLiteOpenHelper extends SQLiteOpenHelper { MySQLiteOpenHelper(Context context) { super(context, "/mnt/sdcard/database_name.db", null, 0); } }

Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead

참고URL : https://stackoverflow.com/questions/4452538/location-of-sqlite-database-on-the-device

반응형