react-native-sqlite-storage icon indicating copy to clipboard operation
react-native-sqlite-storage copied to clipboard

I get "db.transaction is not a function (it is undefined)" on database initialization

Open karlkuhne opened this issue 7 months ago • 2 comments

I am working on my first react native app and settled on sqlite for data management. However, I cant initialize the data bank. I always get the error message

(NOBRIDGE) ERROR Database initialization failed: [TypeError: db.transaction is not a function (it is undefined)]

I have expo-sqlite installed. I would appreciate any help.

import * as SQLite from 'expo-sqlite';

const db = SQLite.openDatabaseAsync('workouttracker.db');

export const init = () => {
    const promise = new Promise((resolve, reject) => {
        db.transaction(tx => {
            tx.executeSql(
                'PRAGMA foreign_keys = ON;',
                [],
                () => { },
                (_, error) => {
                    reject(error);
                    return false;
                }
            );

            tx.executeSql(
                `CREATE TABLE IF NOT EXISTS workout_plans (
                      id TEXT PRIMARY KEY,
                      name TEXT NOT NULL,
                      position INTEGER NOT NULL
                    );`,
                [],
                () => { },
                (_, error) => {
                    reject(error);
                    return false;
                }
            );

            tx.executeSql(
                `CREATE TABLE IF NOT EXISTS exercises (
                      num TEXT PRIMARY KEY,
                      plan_id TEXT NOT NULL,
                      muscle TEXT NOT NULL,
                      title TEXT NOT NULL,
                      img TEXT,
                      sets REAL NOT NULL,
                      reps REAL NOT NULL,
                      weight REAL NOT NULL,
                      FOREIGN KEY (plan_id) REFERENCES workout_plans (id) ON DELETE CASCADE
                    );`,
                [],
                () => { },
                (_, error) => {
                    reject(error);
                    return false;
                }
            );

            resolve();
        },
            (error) => reject(error)
        );
    });
    return promise;
}

export default db;

karlkuhne avatar Mar 11 '25 15:03 karlkuhne