我一直收到“错误代码1:当尝试在React Native中使用SQLite时,显示”无此类表

afdcj2ne  于 2022-12-23  发布在  SQLite
关注(0)|答案(1)|浏览(481)

这是代码。我在其他组件中做其他事情,但问题就在这里,因为我一启动应用程序,就得到[Error: Error code 1: no such table: Movies]。所以我的代码从selectOperation()进入错误。我向其他人寻求帮助,他们做了和我几乎相同的事情,所以我看不出问题是什么。

import 'react-native-gesture-handler';
import { createStackNavigator } from '@react-navigation/stack';
import { StatusBar } from 'expo-status-bar';
import { NavigationContainer } from '@react-navigation/native';
import { StyleSheet, Text, View, SafeAreaView, Button, Alert } from 'react-native';
import Movie from './components/newMovie';
import { useEffect, useState } from 'react';
import MainPage from './components/mainPage';
import uuid from 'react-native-uuid';
import * as SQLite from "expo-sqlite";

const db = SQLite.openDatabase('db.movies')
const Stack = createStackNavigator();
export default function App() {
  const [movies, setMovies] = useState([])
  const [movie, setMovieDetails] = useState(false)

  const selectOperation = async () => {
    db.transaction((tx) => {
     tx.executeSql(
       `SELECT * FROM Movies`,
       [],
       (_, { rows: { _array } }) => setMovies(_array),
       (txObj, error) => console.log(error)
     );
   });
  }

  
  useEffect(() => {
    selectOperation()
    db.transaction(
      (tx)=>{
        tx.executeSql(
            'CREATE TABLE IF NOT EXISTS Movies (id TEXT, title TEXT, description TEXT, duration TEXT, datetime TEXT, actors TEXT)'
            )
          }
        )
      
    }, [])
  return (
    <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen name="Home"
            children={() => <MainPage movies={movies} setMovieDetails={setMovieDetails}/>} />
          <Stack.Screen name="AddMovie" 
            children={() => <Movie movies={movies} setMovies={setMovies} movie={movie}/>}/>
        </Stack.Navigator>
        
    </NavigationContainer>
    
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'rgba(2, 67, 104, 1)',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
c9x0cxw0

c9x0cxw01#

您似乎在useEffect中创建表之前选择了电影(调用selectOperation

相关问题