我使用DB Browser for SQLite預定義了 sqlite 資料庫。我已將 db 檔案放在路徑中root/android/app/src/main/assets/www/mysqlite.db,不幸的是我無法連接。下面是我的版本控制。
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",
我的腳本(我簡化了):
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();
})();
}, []);
}
日志寫道:
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]
我嘗試了多種方法,但無法連接到它。
- 直接從 www 移動到 assets 檔案夾。在手機上卸載應用程式并再次運行。
- 消除
SQLite.enablePromise(false); - react-native 鏈接 react-native-sqlite-storage
- cd android && ./gradlew clean
- 按照步驟打開資料庫呼叫
uj5u.com熱心網友回復:
嘗試從root/android/app/src/main/assets/www/mysqlite.db->移動您的 android 檔案root/android/app/src/main/assets/mysqlite.db
uj5u.com熱心網友回復:
我終于能夠在帶有 Android 11 的三星 Galaxy 上運行它。我已經在帶有 Android 9 的 Redmi6 上嘗試過,它可以運行。
我已經洗掉了包含 SQLite 的 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()"
}
}
}
}
};
我還洗掉了匯入模塊import io.liteglue.SQLitePluginPackage;,MainApplication.java資料庫終于打開了。
我不確定這種方式是否絕對。我希望它是暫時的,因為它反對教程中的方式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/355913.html
