3

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

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

after hours of debugging. This is the solution for me. Previously unable to insert data when using android emulator or ios simulator. Only works on real device. But after removing script like above. Everything works.
this solution saved my job!
but do you know I am searching for a job?. Haha this is the world's reality
feel free to follow me on LinkedIn(linkedin.com/in/vmmoorthy) and twitter(twitter.com/vmmoorthi25)
5

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.

Comments

2

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

Comments

2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.