1

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?

1 Answer 1

2

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);
        }
    );
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.