0

So basically i am trying to insert a new data inside orders but for some reason i can't get new data when i try to select from orders all what i get is old data

i only get new data if i refresh the app otherwise it is showing just old one

import * as SQLite from "expo-sqlite";

function openDatabase() {
  const db = SQLite.openDatabase("db.db");
  return db;
}

const db = openDatabase();



// Create table part

export const createTable = async () => {
  db.transaction((tx) => {
    tx.executeSql(
      "create table if not exists orders (id integer primary key not null, manager int, buyer int, sum int, date text, products text);"
    );
  });

  return {message: "created tables"}
};


// orders part

export const getOrders = new Promise(function(resolve, reject) {
  db.transaction((tx) => {
    tx.executeSql("select * from orders", [], (_, { rows }) => {
      resolve(rows)
    })
  })
});


export const setOrder = (workerId, consumerId, sum, date, products) => {
  db.transaction((tx) => {
    tx.executeSql("insert into orders (manager, buyer, sum, date, products) values (?, ?, ?, ?, ?)", [workerId, consumerId, sum, date, products])
  });
}

honestly i tried everything in my mind but didn't help so i am here for help

  • I'm not up on Expo, I'll admit, but are these "db.transaction" calls actually isolated transactions that need to be specifically committed, or do they auto-commit? (And then, is there some setting/config that controls the auto-commit?) To me, what you're describing (where your data only becomes available after refreshing the app) sounds like those are isolated transactions that are currently not committing anything until the database connections are forced closed (which is probably happening when you "refresh"). Can you try the exec() method and see if you still see the same behaviour? – Craig Jun 13 '23 at 00:06
  • A brief read of the Expo/SQLite docs seems to indicate that maybe the exec() method will just execute statements directly, without using any specific transaction – Craig Jun 13 '23 at 00:07
  • @Craig So i tried with exec, still i get same thing, i can't get new rows by selecting them after insert ` db.exec([{ sql: 'select * from orders', args: [] }], false, (_, results) => resolve(results[0]) );` – GameOverAgain Jun 13 '23 at 00:27
  • did you also do an exec() - rather than transaction() - for the "insert" statement? Also, after you've done the insert - but before you refresh the app - can you query the database separately (not sure whether there's a separate UI that you can query the database with?) and see the new records? Or do the new records actually not appear until the app is cycled? I'm just curious if it is actually a transaction commit issue? – Craig Jun 13 '23 at 02:25
  • @Craig i tried to insert also with exec() but still i don't have new rows i will try to query from same screen and inform you – GameOverAgain Jun 13 '23 at 17:39

0 Answers0