I wanted to know if you could help me with the following error:
I'm trying to remove the EXPO SQLite base in React-Native and then import another base that I have in my folder.
When I do, it throws me the following error:
Error: no such table: users (code 1 SQLITE_ERROR): , while compiling:
this is my code:
import React, { useState, useEffect } from 'react';
const SQLite = require('expo-sqlite')
import * as FileSystem from 'expo-file-system';
import { Asset } from "expo-asset";
var db = null;
export default class SQLiteScreen extends React.Component {
constructor() {
super();
}
/*async openDatabase() {
if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite')).exists) {
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite');
}
await FileSystem.downloadAsync(
Asset.fromModule(require('./MainDB.db')).uri,
FileSystem.documentDirectory + 'SQLite/MainDB.db'
);
db = await SQLite.openDatabase(`MainDB.db`);
}
*/
async openDatabaseIShipWithApp() {
if (!(await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}SQLite/MainDB.sqlite`)).exists) {
await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}SQLite/`, { intermediates: true });
const asset = await Asset.fromModule(require("./MainDB.db"));
await FileSystem.downloadAsync(asset.uri, `${FileSystem.documentDirectory}SQLite/MainDB.sqlite`);
}
db = await SQLite.openDatabase(`MainDB.sqlite`);
}
async deleteDatabaseWithApp(){
const internalDbName = "MainDB.sqlite"; // Call whatever you want
const sqlDir = FileSystem.documentDirectory + "SQLite/";
if ((await FileSystem.getInfoAsync(sqlDir + internalDbName)).exists){
await FileSystem.deleteAsync(sqlDir+internalDbName,
{idempotent:true})
}
}
ExecuteQuery = (sql, params = []) => new Promise(async (resolve, reject) => {
await this.openDatabaseIShipWithApp();
db.exec([{ sql: 'PRAGMA foreign_keys = ON ; ', args: [] }], false, () => { });
db.transaction((trans) => {
trans.executeSql(sql, params, (trans, results) => {
resolve(results);
},
(_, error) => {
reject(error);
});
});
});
}
I call the function as follows:
let query = `SELECT * FROM personal`;
await new SQLiteScreen().deleteDatabaseWithApp();
let selectQuery = await new SQLiteScreen().ExecuteQuery(query);