How to view the creation of Sqlite database and tables in Android Studio

The first way: File Explorer

  • Key point : First, ensure that the computer is connected to an Android machine (mobile phone or tablet, etc.) or open the emulator of the corresponding project in Android Studio.
  • Click View on the menu item above Android Studio
    insert image description here
  • Continue to click Tool Windows
  • Continue to click on Device File Explorer
    insert image description here
  • Jump to the following interface, click in the order of the data/data directory
    insert image description here
  • Determine the package name, see the project directory on the left for the package name: src/main/java/package name
    insert image description here
  • Then find the folder of the package under the above data/data/ folder
    insert image description here
  • Next, the database is in the files folder or databases folder, so how to determine which folder it is in?
    • There is a simple method, generally only one of the two exists, that is the folder that exists
    • But what is more rigorous is to see how to write the save path of the database in the code
      • As far as I know, there are two ways of writing
      • The first method: using a relative path , the database will be saved in the databases folder
        insert image description here
      • The second method: use an absolute path ( this.getFilesDir().toString ), and the database will be saved in the files folder
        insert image description here
  • Therefore, you can find the database in the data/data/package name/files directory or data/data/package name/databases directory in Device File Explorer , as follows:
    insert image description here
    insert image description here
  • There will be two database files, one is the .db3 file we created, and the other .db3-journal file is a dying log file for the database to support things. Usually, the size of this file is 0 words Festival.
  • After finding the database file, we can use some auxiliary tools of Android Studio to open the database file and view it. If we have time in the future, we will continue to study this part.

The second way: adb shell

  • adb is a debugging tool that comes with the Android SDK. Using this tool, you can directly debug the Android machine (mobile phone or tablet, etc.) or emulator connected to the computer. It is stored in the platform-tools directory of the sdk. If you want to use this tool on the command line, you need to configure its path in the environment variable first.

  • If it is a Windows system, you can right-click My Computer->Properties->Advanced->Environment Variables, then find Path in the environment variables and click Edit to configure the platform-tools directory, as shown in the figure.
    insert image description here

  • If it is a Linux system, you can edit the .bash_profile file in the home path and configure the platform-tools directory. Since I am a Windows system, this step will not be shown.

  • After configuring the environment variables, you can use the adb tool.

  • Remind again, before entering the adb debugging mode, you need to ensure that the computer is connected to the Android machine (mobile phone or tablet, etc.) or open the emulator of the corresponding project in Android Studio.

  • Open the command line interface and enter adb shell to enter the console of the device, as shown in the figure.
    insert image description here

  • Then use the cd command to go to the data/data/package name/databases or data/data/package name/files directory, as shown in the figure.
    insert image description here

  • Use the ls command to view the files in the directory
    insert image description here

  • There are two database files in this directory, one is the myDict.db3 file we created, and the other is the myDict.db3-journal file, which is a dying log file for the database to support things. Usually, this file The size is 00 bytes.

  • Next, we use the sqlite command to open the database, just type sqlite3 followed by the database name , as shown in the figure.
    insert image description here

  • At this time, the myDict.db3 database has been opened, and now the tables in this database can be managed. First, let's look at what tables are currently in the database, and type the .table command (note the . in front).
    insert image description here

  • It can be seen that there are two tables in the database at this time, the android_metadata table is automatically generated in each database, don’t worry about it, and the other table is created by us, here you can also pass the .schema command (note that there is .) to view their table language. as the picture shows.
    insert image description here

  • If you want to continue to view and modify the data in databases and tables, you need to learn sqlite commands, and I will write a blog post specifically for this in the future.

  • After that, type .exit (with . in front) or .quit command (with . in front) to exit the editing of the database, and then type exit (without . in front) command to exit the device console.

Personally recommend the second adb debugging method, which is simpler and faster, and the required knowledge of sqlite commands is also necessary for us.

  • In addition, it should be noted that after entering the adb debugging mode, you must exit with .exit and exit , otherwise you will not be able to enter when running the emulator, and you will always be stuck in the following situation.
    insert image description here
  • At present, the method I can think of is: delete the emulator, reconfigure one, and that's it.

Guess you like

Origin blog.csdn.net/weixin_43636084/article/details/123438999