0
export async function insertAttempt() {
    const db = await openDatabase()
    try {
        return await new Promise((resolve, reject) => {
            db.transaction(
                (tx) => {
                    tx.executeSql("INSERT INTO Attempt (attempt_date)
                    VALUES (?)", [Date.now()])
                    tx.executeSql(query.selectAttempt, [], (transaction, resultSet) => {
                        console.log(resultSet)
                    })
                },
                reject,
                resolve
            )
        })
    } catch (e) {
        console.log("Error: ", e)
    }
}

I'm calling the above in a react hook component, like so:

    useEffect(() => {
        async function example() {
            await insertAttempt()
        }

        example()
    }, [])
Error:  [Error: attempt to write a readonly database (code 1032 SQLITE_READONLY_DBMOVED)]

I don't have this problem with select sql operations, only with inserts.

1 Answer 1

1

If you are using a Pre-populated database in your App, go to your openDatabase() function, you may need to access the private attribute _db of WebSQLDatabase and close it with available method, you will end up with something similar to below.

export default async function openDatabase() {

  const database = SQLite.openDatabase("myDb.db")
  database._db.close()
  
    if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + "SQLite")).exists) {
      await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + "SQLite");
    }

    await FileSystem.downloadAsync(
      Asset.fromModule(require("../assets/www/myDb.db")).uri,
      FileSystem.documentDirectory + "SQLite/myDb.db"
    );
    
    return SQLite.openDatabase("myDb.db");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this worked for me. I'm really interested to know why this is (and how you managed to find it out!).

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.