sqlite 为什么会出现“Error:无法打开数据库“”?

5vf7fwbs  于 2023-06-30  发布在  SQLite
关注(0)|答案(5)|浏览(130)

我使用的是Android模拟器,所以我把users.db放在'android/app/src/main/assets/users.db'中。我已经运行了npx react-native link来建立链接。我使用的是React Native 6.0以上的版本,带有自动链接。
我得到了“Error: Could not open database”:

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>
    )
  }
}
u1ehiz5o

u1ehiz5o1#

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

bbmckpt7

bbmckpt72#

您需要将数据库放入assets/www文件夹才能使其工作。

tag5nh1u

tag5nh1u3#

在我从设备上完全卸载应用程序并使用npx react-native run-android从头开始重新安装之后,预填充的数据库是只读的。
仅仅在应用程序已经在设备上时重新加载它是不够的。

2uluyalo

2uluyalo4#

我从react-native.config.js中删除了它,它工作了

"react-native-sqlite-storage": {
  platforms: {
    android: {
      sourceDir:
        "../node_modules/react-native-sqlite-storage/platforms/android-native",
      packageImportPath: "import io.liteglue.SQLitePluginPackage;",
      packageInstance: "new SQLitePluginPackage()"
    }
  }
}

参考:Github

bxfogqkk

bxfogqkk5#

数据库的完整文件路径应为“\android\app\src\main\assets\www”。

相关问题