Expo SQLite无法查询数据库

时间:2019-05-07 23:27:16

标签: react-native expo

我目前在尝试在Expo SQLite中查询数据库时遇到问题。

在我的app.js文件中,我从外部来源获得数据库,如下所示:

// load DB for expo
FileSystem.downloadAsync(
  'http://example.com/downloads/data.sqlite',
  FileSystem.documentDirectory + 'data.sqlite'
)
.then(({ uri }) => {
  console.log('Finished downloading to ', uri)
})
.catch(error => {
  console.error(error);
});

我可以确认数据库已在获得的控制台中下载:

Finished downloading to  file:///var/mobile/Containers/Data/Application/6E1347A8-187E-4EF3-B360-60A0B24E1008/Documents/ExponentExperienceData/%2540anonymous%252Fexpo-sqlite-example-36a92625-6e95-41cc-af6d-a47fc254b237/data.sqlite
this is5507

然后将其加载到另一个组件中

const db = SQLite.openDatabase('data.sqlite');

然后我像这样运行查询:

  componentDidMount() {
    db.transaction(tx => {
      tx.executeSql(
        'SELECT * FROM dr_report_templates',
        [],
        (success) => console.log(success),
        (error) => console.error(error)
      );
    })
  }

我已经确认dr_report_templates在数据库中。 我得到的错误是这样的:

h {
  "_complete": false,
  "_error": null,
  "_running": true,
  "_runningTimeout": false,
  "_sqlQueue": t {
    "first": undefined,
    "last": undefined,
    "length": 0,
  },
  "_websqlDatabase": h {
    "_currentTask": _ {
      "errorCallback": [Function anonymous],
      "readOnly": false,
      "successCallback": [Function anonymous],
      "txnCallback": [Function anonymous],
    },
    "_db": n {
      "_closed": false,
      "_name": "data.sqlite",
    },
    "_running": true,
    "_txnQueue": t {
      "first": undefined,
      "last": undefined,
      "length": 0,
    },
    "version": "1.0",
  },
}

在我的设备上,我得到:

console.error: {"_websqlDatabase": {"version":"1.0","_db":...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

成功和错误回调具有两个参数,第一个是事务,第二个是结果/错误