taher
taher

Reputation: 159

expo-sqlite don't keep data inserted in react native

i create a program that save data to database in react native but when reopen expo and run again program database were deleted and create new empty database

async save() {
          
   if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite')).exists) {
       await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite');
                         
   }
   await FileSystem.downloadAsync(
       Asset.fromModule(require('../assets/factor.db')).uri,
       FileSystem.documentDirectory + 'SQLite/factor.db'
   );
   const db = SQLite.openDatabase('factor.db');
       
   db.transaction((tx) => {
   tx.executeSql("INSERT INTO factors (created_at,customer,sender,pay,off,description,customer_id) 
   values(?,?,?,?,?,?,1)",
   [this.state.date, this.state.customer, this.state.sender, this.state.payment, this.state.offset, 
   this.state.description],
   (trans, rows) => {
        alert('factor added');
        console.log(rows);

        db._db.close();
                            
   },
  (err, msg) => {
       console.log(msg);
  });
  tx.executeSql(
    "SELECT * FROM factors",
    [],
    (trans, res) => {
    console.log(res.rows);

    db._db.close();
                            
  },
  (err, msg) => {
      console.log(msg);
  });
 }, null);
                  

      
}

and after run select sql return one row and dont show prev rows added before

Upvotes: 1

Views: 875

Answers (1)

David
David

Reputation: 35

Responding as I had this issue last week and it took me a while to find the answer. When running expo on my device the database changes are made to the local database on the device, not the pre-populated or blank database included as an asset in my code. Reloading the app without re-running "expo start" preserves the database on the device but stopping the server and re-running "expo start" will initiate a new app build which will overwrite any changes made in previous instances.

Upvotes: 1

Related Questions