为什么我收到“错误:无法打开数据库”?

Den*_*nes 3 sqlite react-native react-native-sqlite-storage

我使用的是 Android 模拟器,因此我将users.db放在“android/app/src/main/assets/users.db”中。我已经跑了npx react-native link建立链接了。我使用的是带有自动链接功能的 React Native 6.0 以上版本。

我收到“错误:无法打开数据库”:

import React, {Component} from 'react'
import {View, Text, Alert} from 'react-native'
import SQLite from 'react-native-sqlite-storage'

export default class App extends Component {

  constructor(props) {
    super(props)
    SQLite.openDatabase({name:'users', createFromLocation:1}, this.connected, this.failed)
  }

  connected= () =>{
    Alert.alert('Connected with success !')
  }

  failed= (e) =>{
    Alert.alert('Something went wrong !', `${e}`)
  }

  render(){
    return(
      <View>
        <Text>Testing SQLite</Text>
      </View>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

Den*_*nes 5

将属性更改createFromLocation为数字 2 时,系统会创建自己的数据库,该数据库为空,因此您需要创建表。不幸的是我没能使用我的旧数据库。