jepiqueau / react-sqlite-app-starter

Ionic/React SQLite Application Starter
MIT License
22 stars 14 forks source link

Is there away to connect to database with sqllite on app.tsx instead of outside app.tsx #9

Closed stealthAngel closed 3 years ago

stealthAngel commented 3 years ago
  sqlite = useSQLite({
    onProgressImport,
    onProgressExport
  });
  useEffect(() => {
    const initialize = async (): Promise<Boolean> => {
      try {
        let db: SQLiteDBConnection = await sqlite.createConnection("myDB");
        await db.open();
        let query = `
        CREATE TABLE IF NOT EXISTS test (
          id INTEGER PRIMARY KEY NOT NULL,
          name TEXT NOT NULL,
        );
        `
        await db.execute(query);
        await db.close();
        return true;
      }
      catch (err: any) {
        return false;
      }
    }
    if(sqlite.isAvailable) {
      initialize().then(async res => {
        if(res) {
        } else {
          console.error('failed');
          console.log(res);
        }
    });
    }
  }, []);

The reason I want this is so I can initialize the tables if they don't exist on startup. Is there any way? Thank you.

This same code without
sqlite = useSQLite({ onProgressImport, onProgressExport }); works if you place it in a place like. tab1.tsx

jepiqueau commented 3 years ago

@stealthAngel There is no reason why you should not. take care that your query is wrong it should be

        let query = `
        CREATE TABLE IF NOT EXISTS test (
          id INTEGER PRIMARY KEY NOT NULL,
          name TEXT NOT NULL
        );

Having change the query it works, tell me if you have still an issue. i will update react-sqlite-app-starter to demonstrate it

stealthAngel commented 3 years ago

@jepiqueau

It still fails, unfortunately. Even if I only call await sqlite.createConnection("myDB") without anything else, it fails.

  sqlite = useSQLite({
    onProgressImport,
    onProgressExport
  });
  useEffect(() => {
    const initialize = async (): Promise<Boolean> => {
      try {
        let db: SQLiteDBConnection = await sqlite.createConnection("myDB");
        return true;
      }
      catch (err: any) {
        return false;
      }
    }
    if(sqlite.isAvailable) {
      initialize().then(async res => {
        if(res) {
        } else {
          console.error('failed');
          console.log(res);
        }
    });
    }
  }, []);

logs failed :(

This code works outside app.tsx. But not within app.tsx.

it's like the sqlite variable is not set only when used outside.

the reason for wanting the sqlite code available in the app.tsx is that I want to check for existing tables only once, otherwise I'd have to do it on every other page.

jepiqueau commented 3 years ago

@stealthAngel i have updated react-sqlite-app-starter 3.2.2-3 and implement your code in the App.ts. For having the Web implementation working you need to update @capacitor-community/sqlite@3.2.2-3 and jeep-sqlite Stencil component to 1.2.1. Voilà

jepiqueau commented 3 years ago

@stealthAngel Did you made some progress? Is it working now? If yes can you close the issue

stealthAngel commented 3 years ago

Thank you so much sir!

This issue is solved.

If you can maybe help me with one last thing. Is the database stored somewhere. I don't know which location.

Love you brother

jepiqueau commented 3 years ago

@stealthAngel location is platform dependent

Hope this will help you in your development Feel free to close the issue

stealthAngel commented 3 years ago

Thank you brother!