How to access existing sqlite database files under android

Source of the problem : SQLite databases under Android are generally stand-alone. When there is a lot of data in the database in an application, it is obviously a waste of time to reconfigure all the data in the database every time you change the machine, so the best way is to Access an existing database.
Solution :
Solution 1 : Operation in Eclipse is limited to simulator use.
step:

  • Open the File Explorer view in Eclipse. File Explorer is used to view the files of the device. The opening method is to execute the [Window]-[Show View]-[other]-[File Explorer] operation in Eclipse.
  • Find the location of the file to be imported, which is the default location of the database/data/data/<package name>/database directory.
  • Select the directory, then click the device icon [Pull a file from the device] in the upper right corner, and then select the database to upload.
    The diagram is as follows:
    Write picture description here

Option 2 : Place the database in the res/raw directory, and copy it to the /data/data/<package name>/database directory when accessing the database for the first time. This is applicable to both simulators and real machines.
The copied code is as follows:

try {
                String filePath = context.getCacheDir().getAbsolutePath();
                String DATABASE_PATH = filePath.substring(0, filePath.length() - 5)
                        + "databases";
                String DATABASE_FILENAME = "usmartscada.db";
                String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
                File dir = new File(DATABASE_PATH);

                if (!dir.exists()) {
                    dir.mkdir();
                }

                if (!(new File(databaseFilename)).exists()) {
                    InputStream is = context.getResources().openRawResource(R.raw.usmartscada);;
                    FileOutputStream fos = new FileOutputStream(databaseFilename);
                    byte[] buffer = new byte[8192];
                    int count = 0;
                    // 开始复制文件
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

The name of the database in the above code example is usmartscada.db. It also proves that the copied database can be read and written normally. There is no problem of insufficient permissions to read and write. However, there are several problems with the above code. .

  1. Why should the existing database files be placed in the res/raw folder? Can they be placed elsewhere?
    If placed in the res/raw folder, the database file must be determined and cannot be changed when the apk is packaged. Otherwise, it will have to be recompiled and generated apk every time, which will be very troublesome when using it, so it is best to place the database file somewhere on the sd card.

    After verification, it is known that the database file can be placed in other locations, such as a certain location on the SD card. As for where to place it, it depends on the specific situation. The above code is just an example. Of course, it can be placed in different locations to access the file. The way of time is also different. Of course, please note that when you want to access the sd card, you must add the permission to access the sd card in the AndroidManifest.xml file.

  2. The above code does not determine whether the existing database file exists? One is for the sake of the rigor of the code, and the other is because the existing database may not be needed on a special machine. You want to configure and generate the database yourself. In this case, there is no need to copy the previous database.
    This question depends on the actual situation to determine whether it is necessary to judge whether the existing database file exists. The judgment depends on the location where the database file is placed. In addition, if there is no way to judge whether the database file exists in the res/raw folder, because The method used to access the files in the res/raw folder is R.raw.xxx. If the file does not exist, there is no way to get the object of the database file, so there is no way to judge. I will add some knowledge about the files in the res/raw folder. The absolute path is "android.resource://" + getPackageName() + R.raw.xxx.

Guess you like

Origin blog.csdn.net/li1500742101/article/details/48519841