Seeding 300mb Sqlite database for react native?

209 Views Asked by At

Problem

I have a large sqlite db (~300mb) and I want to use it to seed my application on load and store to the filesystem to use sqlite queries. For context, it is a language dictionary application.

I have looked around, and I haven't found a solution that is applicable to my situation. Ideally, I understand that I can use the importFromJson function from the @capacitor-community/sqlite" plugin.

The issue with this is, the file seems to be too large + not being able to find applicable examples to implement properly since this is all new to me.

I am following this example : https://github.com/jepiqueau/react-sqlite-app-starter/blob/02809174e3b5bb355e506ed3bf20e1a92c74f860/src/pages/Tab1.tsx#L22

const retImport: any = await sqlite.importFromJson(JSON.stringify(jsonImport));

this line here, If I stringify my data, I am sure this is not going to work since the data would consume too many resources.

Expected Results

I want to be able to import large (~300mb) sqlite OR JSON files to seed my react ionic database to use with Sqlite.

Is there a way I can stream the json file, upload it in chunks? I am aware how to do with fs but I don't know to do with react native + ionic + @capacitor-community/sqlite.

1

There are 1 best solutions below

0
appleman On

ok! So I solved this for importing a .sqlite db dump for react ionic native using that capacitor sqlite community plugin.

How to import and use a .sqlite file

  1. Export your file from your sqlite3 database

  2. copy that file into your public/assets/databases folder as <name>.db. Make sure to change the extension to .db AND also add the definition in your databases.json file in the same directory

    { "databaseList": [ "test_db.db" ] }

  3. Before you connect to the db by name, use the function await sqlite.copyFromAssets(); after importing import { sqlite, existingConn } from '../App'; from the App.tsx file as mentioned in the boiler plate I linked before. Please refer to the App.tsx + Index.tsx files.

  4. Create the connection and Connect to the db

  const db = await sqlite.createConnection("<db_name>", false, "no-encryption", 1);
  await db.open();
  1. Then, you should be able to query this database assuming you know the names of the tables you're querying.
let values = await db.query(`SELECT * FROM <table_name> LIMIT 10;`)