Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
Expo SQLite Database isn't initialising in react native

I am trying to create Database for my react native app using expo's SQLite and followed its documentation for it but I think Database isn't initializing.

Documentation: expo-sqlite

App.js:

console.log("init () above");

init()
  .then(() => {
    console.log("DATABASE INITIALIZED");
  })
  .catch(() => {
    console.log("FAILED DATABSE INITIALIZATION", err);
  });

console.log("init () below");

//Nothing logs in console

Console:

init () above
init () below
places: Array []

I created the promise as init() which returns an error if it fails and when it resolves it logs a success message DATABASE INITIALIZED. But I am not getting any output.

helper/db:

import * as SQLite from "expo-sqlite";

const db = SQLite.openDatabase("places.db");

export const init = () => {
  const promiseDB = new Promise((resolve, reject) => {
    db.transaction((tx) => {
      tx.executeSql(
        "CREATE TABLE PLACES IF NOT EXISTS places (id INTEGER PRIMARY KEY NOT NULL, title TEXT NOT NULL, imageUri TEXT NOT NULL, address TEXT NOT NULL, lat REAL NOT NULL, lng REAL NOT NULL)"
      );
      [],
        () => {
          resolve();
        },
        (_, err) => {
          reject(err);
        };
    });
  });
  return promiseDB;
};


I am not getting any errors and also not the success message in the console. I don't know what's the issue and what I am doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You didn't write your function correctly, you forgot a curly bracket and added and empty array of arguments where you shouldn't have.

Also in your query you wrote CREATE TABLE PLACES IF NOT EXISTS places. It believe the proper syntax would be CREATE TABLE IF NOT EXISTS places.

The order of arguments in transaction is callback, errorCallback, successCallback. https://docs.expo.dev/versions/latest/sdk/sqlite/#database

The order of the arguments in executeSql is sqlStatement, arguments, callback, errorCallback. https://docs.expo.dev/versions/latest/sdk/sqlite/#sqltransaction

This should create your table:

return new Promise((resolve, reject) => {
        db.transaction(
            function (tx) {
                tx.executeSql("CREATE TABLE IF NOT EXISTS places (id INTEGER PRIMARY KEY NOT NULL, title TEXT NOT NULL, imageUri TEXT NOT NULL, address TEXT NOT NULL, lat REAL NOT NULL, lng REAL NOT NULL)");
            },
            function (error) {
                reject(error.message);
            },
            function () {
                resolve(true);
                console.log('Created database OK');
            }
      );
});

An exemple for when you need to add arguments:

return new Promise((resolve, reject) => {
    db.transaction(
        function (tx) {
            tx.executeSql(
                'SELECT * FROM TABLE_NAME WHERE name=?;',
                [name],
                function (tx, resultSet) {
                    let data = [];
                    for (let i = 0, c = resultSet.rows.length;i < c;i++) {
                        data.push(resultSet.rows.item(i));
                    }
                    resolve(data);
                },
                function (tx, error) {
                    reject(error.message);
                }
            );
        },
        function (error) {
            reject(error.message);
        }
    );
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda