Inserting 1000000 rows in sqlite3 database

前端 未结 3 1118
有刺的猬
有刺的猬 2020-12-30 17:37

I want to insert 10,00,000 rows into the database, but the it takes too long time time in insertion like.

e.g. Now I am trying it with 2055 rows and it takes 3 minut

3条回答
  •  孤城傲影
    2020-12-30 17:47

    You could populate your database using offline tools and then import it when you install your package. You can either store the database in the external sd card or in the asset folder of your application.

    This is how I do it:

    1. Copy the application database to a local folder using the Android Debuger Bridge (adb) like this: adb pull /data/data//databases/yourdatbase.db C:/users/databases/yourdatbase.db.

    2. Connect to the SQLites database C:/users/databases/yourdatbase.db with your favourite GUI/CLI tool and complete your population of the 1 000 000 records.

    3. Copy your populated database to your Android development environment asset folder.

    4. Now uninstall your application from the device to make sure there is no database created when you install for the first time.

    5. Modify your SQLiteHepler class so that it checks if a database exists and if one exists it uses that one. If no database exists the Helper copies the one from your asset folder together with your 1 000 000 records. This is how I have done it:

      public class MyDatabaseHelper extends SQLiteOpenHelper {
      
          /*
              Other SQLiteOpenHelper declarations here ...
          */
      
          private static final String DATABASE_NAME   = "application.db";
          private static final String DB_PATH         = "/data/data/" + context.getPackageName() + "/databases/";
      
      
          /*
              Your SQLiteOpenHelper functions/procedures here ...
          */
      
          public boolean isDataBaseExist() {
      
              File dbFile     = new File(DB_PATH + DATABASE_NAME);
              return dbFile.exists();
          }
      
          public void copyDataBase(Context context) throws IOException {
      
              this.getReadableDatabase();
      
              InputStream inFile      = context.getResources().getAssets().open(DATABASE_NAME);
      
              // Path to the just created empty db
              String outFileName      = DB_PATH + DATABASE_NAME;
              OutputStream outFile    = new FileOutputStream(outFileName);
      
              // transfer bytes from the inputfile to the outputfile
              byte[] buffer = new byte[1024];
      
              int length;
      
              while ((length = inFile.read(buffer)) > 0) {
      
                  outFile.write(buffer, 0, length);
      
              }
      
              // Close the streams
              outFile.flush();
              outFile.close();
              inFile.close();
          } 
      

    This database will be compliled with you app and on first launch all the data will be there. There could be a simpler method, but I hope this helps someone.

提交回复
热议问题