无法打开数据库 – SQLite React Native

huangapple go评论51阅读模式
英文:

Could not Open Database - SQLite React Native

问题

我有一个React Native应用程序,它与本地数据库建立连接。数据库位于名为OA-DB.db的assets/www文件夹中。
这是用于打开数据库连接的const:

const db = openDatabase({ name: 'OA-DB.db', createFromLocation: 1 }, () => console.log('Connected'), (error) => console.log('Error:' + error));

这个连接在Android 11、Android 13和Android 14设备上工作得很好,除了一个特定的设备,一个搭载Android 13和Samsung One Ui 5.1的Android平板,它在尝试打开连接时返回错误:Error: Could not open database。
我尝试了很多方法,将createFromLocation更改为1,禁用了工作帐户,因为在工作帐户启用时,它不允许在Android Studio上查看文件。
所以现在我可以使用Android Studio看到安装文件夹和文件,我可以看到数据库文件夹,里面有数据库文件。
有人可以给我一些关于这个问题的指导吗?

更新

我尝试了一些其他的方法,我确定数据库存在于文件夹中,连接中的数据库名称是正确的,这表明文件在安装期间已经正确捆绑到应用程序中。
我已经在许多设备上安装了这个应用程序,但错误只在这个特定的设备上出现。

更新2

现在我尝试了另一种解决方法,使用react-native-fs将数据库复制到设备内的另一个文件夹,我成功地将数据库复制到了另一个文件夹,但仍然无法连接到该设备上的数据库。

复制数据库的函数

async function copyDatabaseIfNeeded() {
  const dbPath = '/storage/emulated/0/Documents/' + `${dbName}`;
  console.log('dbPath:' + dbPath);
  console.log('dbLocation:' + dbLocation);
  const dbExists = await RNFS.exists(dbPath);
  if (!dbExists) {
    try {
      // 检查是否已授予外部存储权限(仅适用于Android)
      if (Platform.OS === 'android') {
        const granted = await requestExternalStoragePermission();
        if (!granted) {
          console.error('Storage permission not granted.');
          return;
        }
      }
      // 构建资产的正确源路径
      const sourcePath = Platform.OS === 'android' ? 'www/onlineaudits_new.db' : dbName;
      await RNFS.copyFileAssets(sourcePath, dbPath);
      console.log('Database copied successfully');
    } catch (error) {
      console.error('Error copying database:', error);
    }
  }
}

打开连接的函数

// 打开数据库连接
function openDatabase() {
  return new Promise((resolve, reject) => {
    SQLite.openDatabase(
      { name: dbName, location: dbLocation },
      (db) => {
        console.log('Database connection opened successfully');
        resolve(db);
      },
      (error) => {
        console.error('Error opening database:', error + dbLocation);
        reject(error);
      }
    );
  });
}
英文:

I have a React Native app which opens connection with local database. The database is placed inside the assets/www folder called OA-DB.db.
This is the const to open the database connection:

const db = openDatabase({  name: 'OA-DB.db',  createFromLocation: 1,}, () => console.log('Connected'), (error) => console.log('Error:' + error));

This connection is working well on Android 11, Android 13, Android 14 devices. Except one specific device an Android tablet with Android 13 and Samsung One Ui 5.1, that returns Error while tries to open the connection:Error: Could not open database.
I've tried a lot of things, changed the createFromLocation to 1, disabled the Work account, because with the work account on, it doesn't allow to see files on Android Studio.
So now I can see the installation folders and files with Android Studio, and I can see the database folder and inside of it the database file.
Can anyone give me some guidance on this?

Updates

I've tried some more things, I'm sure the database is present at the folder, the name of the database is correct at the connection, so indicates that the file is being properly bundled with the app during installation.
I have this app installed on many devices, but the error only occurs on this specific.

Update 2

Now I've tried another work around, copy the database to another folder inside the device, using the react-native-fs, I was able to copy the database to other folder, but still not able to connect with the database at this device.

function to Copy the Database

async function copyDatabaseIfNeeded(){
  const dbPath = '/storage/emulated/0/Documents/'+`${dbName}`;
  console.log('dbPath:'+dbPath);
  console.log('dbLocation:'+dbLocation);
  const dbExists = await RNFS.exists(dbPath);
  if (!dbExists) {
    try {
      // Check if external storage permission is granted (only for Android)
      if (Platform.OS === 'android') {
        const granted = await requestExternalStoragePermission();
        if (!granted) {
          console.error('Storage permission not granted.');
          return;
        }
      }
      // Construct the correct source path for the asset
      const sourcePath = Platform.OS === 'android' ? 'www/onlineaudits_new.db' : dbName;      
      await RNFS.copyFileAssets(sourcePath, dbPath);
      console.log('Database copied successfully');
    } catch (error) {
      console.error('Error copying database:', error);
    }
  }
}

Function to Open Connection

// Open the database connection
function openDatabase() {
  return new Promise((resolve, reject) => {
    SQLite.openDatabase(
      { name: dbName, location: dbLocation },
      (db) => {
        console.log('Database connection opened successfully');
        resolve(db);
      },
      (error) => {
        console.error('Error opening database:', error + dbLocation);
        reject(error);
      }
    );
  });
}

答案1

得分: 0

你尝试过使用另一个库来连接数据库吗?

英文:

Have you tried to use another library to connect with the database?

huangapple
  • 本文由 发表于 2023年7月18日 01:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定