17 February 2024

To retrieve rows from a SQLite database in a React Native Expo project, you can use the expo-sqlite package along with JavaScript's asynchronous capabilities.

Source code viewer
  1. import * as SQLite from 'expo-sqlite';
  2.  
  3. // Open a database connection
  4. const db = SQLite.openDatabase('your_database_name.db');
  5.  
  6. // Function to retrieve table rows
  7. const getTableRows = () => {
  8. return new Promise((resolve, reject) => {
  9. db.transaction(tx => {
  10. tx.executeSql(
  11. 'SELECT * FROM your_table_name',
  12. [],
  13. (_, { rows }) => resolve(rows),
  14. (_, error) => reject(error)
  15. );
  16. });
  17. });
  18. };
  19.  
  20. // Example usage
  21. getTableRows()
  22. .then(rows => {
  23. // Access rows here
  24. console.log('Rows:', rows);
  25. })
  26. .catch(error => {
  27. console.error('Error:', error);
  28. });
Programming Language: ECMAScript