Reputation: 105
I want to use SQLite with my Expo react native project. So I downloaded DB browser for SQLite and created the database and tables before loading them into my project.
Now I have the pre-populated database that I want to load into my project. I had several problems like this database doesn't exist ...etc. However I solved it by having a database module:
// Database.js
import * as SQLite from 'expo-sqlite';
module.exports = SQLite.openDatabase('myDb.db');
And then import it in my Home Page in Screens folder:
import Database from '../db/database';
function myFunction(){
console.log("Enter Function");
console.log(Database);
Database.transaction(function(txn) {
console.log("Enter Transaction");
txn.executeSql(
"INSERT INTO users (name, email, password) VALUES ('Lama', 'lama@gmail,com', 'lam123');",
); //end txn
}, function (error){
console.log(error);
},
function(){
console.log("Success");
});
}//end myFunction()
The console logs:
Enter Function
WebSQLDatabase {
"_currentTask": null,
"_db": SQLiteDatabase {
"_closed": false,
"_name": "myDb.db",
},
"_running": false,
"_txnQueue": Queue {
"length": 0,
},
"exec": [Function anonymous],
"version": "1.0",
}
Enter Transaction
Error code 1: no such table: users
- node_modules/expo-sqlite/build/SQLite.js:36:15 in _deserializeResultSet
* [native code]:null in map
- node_modules/expo-sqlite/build/SQLite.js:16:40 in SQLiteDatabase#exec
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in setImmediate$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:146:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:194:17 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:458:30 in callImmediates
* [native code]:null in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:407:6 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:143:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:142:17 in __guard$argument_0
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue
I don't really know what I'm missing here, I have tried the code to create Another database and it works and adds records (although I can't find where the database is located in my laptop). I also tried to add a record from the DB browser to myDb and it works:
I found this question asked before but there are not helping answers: SQLite Expo API, no such table in pre-populated file?
Thank you in advance!
Upvotes: 7
Views: 3567
Reputation: 347
i guess that
SQLite.openDatabase('myDb.db');
is opening a database named myDb.db located in the app file system under the path : ${FileSystem.documentDirectory}SQLite/myDb.db
.
so if there is no such file with name 'myDB.db', sqlite will create a new database with same name,
that's why you can't fin your table users in your new created database.
So to ensure that your populated database is in the correct path, you need to download it to your app using FileSytem ,
so you may need to do somthing like that:
FileSystem.downloadAsync(
Asset.fromModule(require(path)).uri,
`${FileSystem.documentDirectory}SQLite/myDb.db`
)
.then(function(){
var db = SQLite.openDatabase('db.db')
// do whatever else you need here
})
.catch(error =>{
console.error(error)
})
Upvotes: 3