React-Native-Sqlite存储无法打开数据库

发布于 2025-02-02 00:49:53 字数 2384 浏览 4 评论 0原文

我们测试sqlite的每个设备。我尝试了我能想到的所有可能组合,并且它总是只返回无法打开数据库。关于如何调试的任何想法?这些只是我尝试过的2个子集的一小部分,

SQLite.openDatabase(
    {
         name: 'my_db.db',
         createFromLocation: 1,
    },
    
    () => { 
        console.log("OPEN DATABASE SUCCESS : ")
    },
    error => {
        console.log("OPEN DATABASE ERROR : " + error);
    }
);

我们尝试了1、3、4,默认值,库,数据库,/data/data/<; /my_db.sqlite

的格式

 SQLite.openDatabase({name: 'test.db', createFromLocation : path, location: 1}, this.openCB, this.errorCB);
SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, this.openCB, this.errorCB);

还以〜/test.db的方式尝试了诸如路径之类

var RNFS = require('react-native-fs');
var path = RNFS.DocumentDirectoryPath + '/my_db.db';

,也可以从我们可以找到的互联网的每个角落进行了大约100件事。还应该指出 rnfs.writefile(路径,'');工作完全很好。除Galaxy S10以外,还可以在每个设备和模拟器上使用。我想念什么愚蠢的东西?

编辑: 我进行了所有反应本机FS位置的输出。 /数据/数据不是选项。哪个应该是正确的版本?

 LOG  CachesDirectoryPath /data/user/0/com.<app_name>/cache
 LOG  DocumentDirectoryPath /data/user/0/com.<app_name>/files
 LOG  DownloadDirectoryPath /storage/emulated/0/Download
 LOG  ExternalCachesDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/cache
 LOG  ExternalDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/files
 LOG  ExternalStorageDirectoryPath /storage/emulated/0
 LOG  FileProtectionKeys undefined
 LOG  LibraryDirectoryPath undefined
 LOG  MainBundlePath undefined
 LOG  PicturesDirectoryPath /storage/emulated/0/Pictures
 LOG  TemporaryDirectoryPath /data/user/0/com.<app_name>/cache

edit2: 我添加了代码首先创建一个数据库文件夹,然后如果成功尝试触摸数据库文件,则用空字符串编写文件。所有这些都是成功的。但是Sqlite Open仍然失败。

RNFS.mkdir(DirectoryPath).then((result) => {
  console.log('Wrote folder!', result);     
  const dvfile = 'my_db.db';
  console.log("Final db location = "+DirectoryPath+dvfile);
  RNFS.writeFile(DirectoryPath+dvfile, "").then((result) => {
     console.log("Was able to touch db file!?\n");
      SQLite.openDatabase({name: dvfile, createFromLocation : DirectoryPath+dvfile, location: DirectoryPath+dvfile}, this.openCB, this.errorCB);
  }).catch((err) => {
     console.warn('Failed to touch the db file', err)
  })
            
}).catch((err) => {
    console.warn('Failed to write folder', err)
 })

Every device we test SQLite.openDatabase on works fine except galaxy s10. I have tried every possible combination I can think of and it always just returns failed to open database. Any ideas on how to debug this? These are only a small subset of what I tried

SQLite.openDatabase(
    {
         name: 'my_db.db',
         createFromLocation: 1,
    },
    
    () => { 
        console.log("OPEN DATABASE SUCCESS : ")
    },
    error => {
        console.log("OPEN DATABASE ERROR : " + error);
    }
);

in place of the 2 we tried 1, 3, 4, default, library, databases, /data/data/<app_name>/my_db.db data/data/<app_name>/my_db.sqlite

also tried in the formats like

 SQLite.openDatabase({name: 'test.db', createFromLocation : path, location: 1}, this.openCB, this.errorCB);
SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, this.openCB, this.errorCB);

the path was tried with ~/test.db also with

var RNFS = require('react-native-fs');
var path = RNFS.DocumentDirectoryPath + '/my_db.db';

And about 100 more things from every corner of the internet we could find. It should also be noted that
RNFS.writeFile(path, ''); works totally fine. Again works on every device and emulator except galaxy s10. What stupid thing am I missing?

EDIT:
I did an output off all the react native fs locations. /data/data wasn't an options. Which is supposed to be the correct version?

 LOG  CachesDirectoryPath /data/user/0/com.<app_name>/cache
 LOG  DocumentDirectoryPath /data/user/0/com.<app_name>/files
 LOG  DownloadDirectoryPath /storage/emulated/0/Download
 LOG  ExternalCachesDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/cache
 LOG  ExternalDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/files
 LOG  ExternalStorageDirectoryPath /storage/emulated/0
 LOG  FileProtectionKeys undefined
 LOG  LibraryDirectoryPath undefined
 LOG  MainBundlePath undefined
 LOG  PicturesDirectoryPath /storage/emulated/0/Pictures
 LOG  TemporaryDirectoryPath /data/user/0/com.<app_name>/cache

Edit2:
I added code to first create a databases folder, then if successful try touching the database file, byt writing a file with an empty string. All of which were successfuly. But the sqlite open still fails.

RNFS.mkdir(DirectoryPath).then((result) => {
  console.log('Wrote folder!', result);     
  const dvfile = 'my_db.db';
  console.log("Final db location = "+DirectoryPath+dvfile);
  RNFS.writeFile(DirectoryPath+dvfile, "").then((result) => {
     console.log("Was able to touch db file!?\n");
      SQLite.openDatabase({name: dvfile, createFromLocation : DirectoryPath+dvfile, location: DirectoryPath+dvfile}, this.openCB, this.errorCB);
  }).catch((err) => {
     console.warn('Failed to touch the db file', err)
  })
            
}).catch((err) => {
    console.warn('Failed to write folder', err)
 })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2025-02-09 00:49:54

尝试const db = sqlite.opendatabase('db.db');在同一目录中使用db.db。然后,尝试执行一个小SQL CMD。

 db.transction(
   (tx) => tx.executeSql("create table name;"),
   (e) => console.error(e),
   () => console.log("success")
 );

Try const db = SQLite.openDatabase('db.db'); with db.db in the same directory. Then, try to execute a small sql cmd.

 db.transction(
   (tx) => tx.executeSql("create table name;"),
   (e) => console.error(e),
   () => console.log("success")
 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文