SQLite-expo return nothing

59 Views Asked by At

When i try to connect and then get data from db i get nothing but i have records in database (SQLiteStudio records proof) I try all guids from Ethernet: get path with filesystem, use other derectory or only db name but it don`t help. This is my directory screenshot .

My code

ExercisePage.js

import React, { useEffect, useState } from "react";
import * as SQLite from "expo-sqlite";
import { StyleSheet, View, Text } from "react-native";

const db = SQLite.openDatabase("../SQLite/data.db");

export default function ExercisePage() {
  const [exercises, setExercises] = useState([]);

  useEffect(() => {
    db.transaction((tx) => {
      tx.executeSql(
        "SELECT exercise.name AS exerciseName, GROUP_CONCAT(tag.name) AS tagNames " +
          "FROM exercise " +
          "LEFT JOIN exercise_tag ON exercise.id = exercise_tag.exercise_id " +
          "LEFT JOIN tag ON exercise_tag.tag_id = tag.id " +
          "GROUP BY exercise.id",
        [],
        (_, { rows }) => {
          const exercisesData = rows._array.map((row) => {
            return {
              name: row.exerciseName,
              tags: row.tagNames ? row.tagNames.split(",") : [],
            };
          });
          setExercises(exercisesData);

          console.log(exercisesData);
        },
        (_, error) => {
          console.error("Error fetching exercises:", error);
        }
      );
    });

    return () => db.close();
  }, []);

  return (
    <View style={styles.MainBox}>
      {exercises.map((exercise, index) => (
        <React.Fragment key={index}>
          <Text>{exercise.name}</Text>
          {exercise.tags.map((tag, tagIndex) => (
            <Text key={tagIndex}>{tag}</Text>
          ))}
        </React.Fragment>
      ))}
    </View>
  );
}

metro.config.js

const { getDefaultConfig } = require('expo/metro-config');

const defaultConfig = getDefaultConfig(__dirname);

defaultConfig.resolver.assetExts.push('db','sqlite');

module.exports = defaultConfig;

module.exports = {
    resolver: {
      assetExts: ['db', 'sqlite', 'ttf', 'obj', 'png', 'jpg', 'otf', 'ttc'],
    },
  };
  

Thanks for help.

0

There are 0 best solutions below