andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.75k stars 521 forks source link

Why database cannot open on Android 11? #496

Closed Applizone closed 2 years ago

Applizone commented 2 years ago

I have install and tried several ways but still unable to connect database. My database is pre-defined that I create using DB Browser.

Expected Behavior

Database should be able to open at the very first place.

Current Behavior

Database cannot open

Possible Solution

Steps to Reproduce (for bugs)

import SQLite from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(false);

export const AppSignIn = (props) => {
    const OpenDB = () => {
        return new Promise((resolve, reject) => {
            global.db = SQLite.openDatabase(
                {
                    name: 'mysqlite.db',
                    createFromLocation: '~mysqlite.db',
                },
                () => {
                    console.log("Connection success!");
                },
                error => {
                    console.log(error);
                    reject();
                });
            resolve();
        });
    }
    const ReadDB = () => {
        return new Promise((resolve) => {
            global.db.transaction(function (tx) {
                tx.executeSql(
                    // The rest of the trx
                );
                resolve();
            });
        });
    }
    async function ConnectDB() {
        return new Promise(async (resolve, reject) => {
            await OpenDB()
                .then(async () => {
                    await ReadDB()
                        .then(() => {
                            console.log('YEAY FINALLY');
                            resolve();
                        })
                })
                .catch((error) => {
                    console.log(error);
                    reject();
                });
        });
    }
    React.useEffect(() => {
        (async () => {
            await ConnectDB()
                .then()
                .catch();
        })();
    }, []);
}

I have post in StackOverflow too

Context

I cannot start any transaction

Your Environment

Samsung Galaxy Android 11, "react-native-sqlite-storage": "^6.0.1", "react": "17.0.2", "react-native": "0.65.1", "@react-navigation/native": "^6.0.4",

Debug logs Android 11 (Samsung Galaxy A71)

LOG OPEN database: mysqlite.db LOG SQLite.open({"name":"mysqlite.db","createFromLocation":"~mysqlite.db","dblocation":"nosync","assetFilename":"~mysqlite.db"}) LOG new transaction is waiting for open operation LOG Phone connected? true, Server connected? true LOG OPEN database: mysqlite.db failed, aborting any pending transactions LOG [Error: Could not open database]

Debug logs Android 9 (Redmi 6)

LOG OPEN database: mysqlite.db LOG SQLite.open({"name":"mysqlite.db","createFromLocation":"~mysqlite.db","dblocation":"nosync","assetFilename":"~mysqlite.db"}) LOG new transaction is waiting for open operation LOG Phone connected? true, Server connected? true LOG Connection success! LOG SQLite.backgroundExecuteSqlBatch({"dbargs":{"dbname":"mysqlite.db"},"executes":[{"qid":1111,"sql":"BEGIN","params":[]},{"qid":1111,"sql":"SELECT * FROM MSCRAP","params":[]}]}) LOG YEAY FINALLY LOG item: 0 LOG [] LOG SQLite.backgroundExecuteSqlBatch({"dbargs":{"dbname":"mysqlite.db"},"executes":[{"qid":1111,"sql":"COMMIT","params":[]}]})

Applizone commented 2 years ago

At targetting SDK 30, what I've done is removing content from react-native.config.js


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

I also removing import io.liteglue.SQLitePluginPackage; from MainApplication.java and the database can open finally.

castalonirenz commented 2 years ago

At targetting SDK 30, what I've done is removing content from react-native.config.js


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

I also removing import io.liteglue.SQLitePluginPackage; from MainApplication.java and the database can open finally.

FTS5 will not work if you do this one