Why do I get "Error: Could not open database"?

5k views Asked by At

I'm using the Android emulator so I put users.db in 'android/app/src/main/assets/users.db'. I already ran npx react-native link to make the link. I'm using a React Native version above 6.0 with auto-link.

I'm getting "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>
    )
  }
}
5

There are 5 answers

0
Denis Ximenes On BEST ANSWER

When changing the attribute createFromLocation to number 2 the system creates its own database which comes empty, so you need to create the tables. Unfortunately I didn't manage to use my old database.

0
Амартүвшин Батаа On

You need to put your db into assets/www folder to make it works.

0
Ger On

The pre-populated database was read only after I completely uninstalled the app from the device and re-installed it from scratch with npx react-native run-android.

It was not enough to simply reload the app while it was already on the device.

0
mikest31 On

The full file path for the database should be "\android\app\src\main\assets\www".

4
VMMOORTHY On

I removed this from the react-native.config.js and it worked

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

Ref : Github