SQLite executeSql doesn't call succes and arrow functions

Hi !
I’m currently learning React Native an so using expo which I love but I am facing a wall right now…
I want to loop through data located in a database (using expo-sqlite).
So I firstly download the database like this:

export function downloadDatabases() {
      FileSystem.downloadAsync(
        Asset.fromModule(require('../assets/db/badic.db')).uri,
        `${FileSystem.documentDirectory}SQLite/badic.db`
      )
}

Then, I have a function in order to loop through the database:

export function importDatabases() {
  let db = SQLite.openDatabase('badic.db');
        
  db.transaction(tx => {
    console.log("This is printed");
    tx.executeSql(`SELECT * FROM test`, [], (tx, results) => {
        console.log("Never printed");
    }),
    (tx, error) => {
      console.log("Never printed either");
    }
  })
}

As you can see, neither the success or the error arrow functions are getting called. It is exactly the same problem as here: https://www.gitmemory.com/issue/expo/expo/5270/520480859 but the solution given doesn’t solve my case.

My “expo diagnostics”:

Expo CLI 3.1.2 environment info:
System:
OS: Linux 4.4 Ubuntu 18.04.3 LTS (Bionic Beaver)
Shell: 4.4.20 - /bin/bash
Binaries:
Node: 13.0.1 - ~/.nvm/versions/node/v13.0.1/bin/node
npm: 6.12.0 - ~/.nvm/versions/node/v13.0.1/bin/npm
npmPackages:
expo: ^35.0.0 => 35.0.0
react: ^16.8.3 => 16.8.3
react-native: https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz => 0.59.8
react-navigation: ^4.0.10 => 4.0.10
npmGlobalPackages:
expo-cli: 3.9.0

I call the downloadDatabase function in componentDidMount() and importDatabase() when I press a button component.
I hope you will understand what I mean. Thank you for all the help you will give me.

Hi @theoker

(I’ve moved your post to the Expo SDK category, since it seems to me to fit better there.)

I think (at least part of) the problem is that you have a misplaced close parenthesis:

Maybe this will give you more clues as to what’s going wrong:

export function importDatabases() {
  let db = SQLite.openDatabase("badic.db");

  db.transaction(
    tx => {
      console.log("This is printed");
      tx.executeSql(
        `SELECT * FROM test`,
        [],
        (tx, results) => {
          console.log("Executed query");
        },
        (tx, error) => {
          console.log("Could not execute query");
        }
      );
    },
    error => {
      console.log("Transaction error");
    },
    () => {
      console.log("Transaction done");
    }
  );
}
1 Like

Ok so thank you very much it solves my problem and thank you for having moved my post to the correct category :)! But… it prints “Could not execute query” everytime. Do you have an idea where the problem could come from ? Maybe the way I “download” my database from the assets is not right ?
The sql statement is correct because when I type it in DB Browser for SQLite it returns the rows of the test table but if I print the error message it prints “no such table: test (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT * FROM test, (OS error - 11:Try again)” I don’t understand why it can’t find such a table.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.