I get "db.transaction is not a function (it is undefined)" on database initialization
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;
did u find a solution please ?
@Essefiamel No, I did not. I went with AsyncStorage instead. Dont know how it will hold up though
This may due to expo-sqllite version update, if you are upgraded expo sdk and expo-sqllite , please see the documentation in Expo https://docs.expo.dev/versions/latest/sdk/sqlite/ , which gives hints to use database functions.