andpor / react-native-sqlite-storage

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

Cannot get data from the pre-populated sqlite database in react native #573

Open nawazokz opened 4 months ago

nawazokz commented 4 months ago

I spend almost 10 hour just to get data from the pre populated database. I place my db file for android in \android\app\src\main\assets i am using react native version 0.73.4

here is my code import SQLite from 'react-native-sqlite-storage'; const initializeDatabase = async () => { try {

  const db = await SQLite.openDatabase({
    name: 'kitabosunnat.db',
    createFromLocation: '~kitabosunnat.db',
  });

  await db.transaction(tx => {
    tx.executeSql(
      'SELECT * FROM Favrouit',
      [],

      (tx, results) => {
        const temp = [];
        const rows = results.rows;
        for (let i = 0; i < rows.length; i++) {
          console.log('Row:', rows.item(i));
          temp.push(rows.item(i));
          // Process and use the data as needed
        }
        setData(temp);
      },
      error => {
        console.error('Error executing SQL query:', error);
      },
    );
  });
} catch (error) {
  console.error('Error initializing database:', error);
}

}; useEffect(() => { initializeDatabase(); console.log(data); }, []); with all the solution i searched on google i still have the following problem

Error executing SQL query {"code": 0, "message": "no such table: categories (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM categories"}

Note: table is present in the database with correct spelling i have tried all the solution but cannot succeed

alihasnain3 commented 2 months ago

@nawazokz were you able to find any solution for this issue?