提取功能导致SQL Transient服务器行为异常

时间:2020-09-16 18:33:34

标签: android reactjs

早上好, 我最近遇到了一个问题,即我已经在自己开发的应用程序中运行SQL脚本。该应用程序使用Transient数据库,并且已经完美运行了一段时间。

我本来想要发生的事情是,该应用程序使用获取过程来执行相关服务器内容的列表,并将它们呈现为Fragment(但是我还没走到那一步)。当DB有点奇怪时,我正处于代码开发的中间。

似乎一夜之间,我似乎已经失去了对数据库的控制权,由于我无法理解的原因,数据库会自动反复向数据库中插入数百个空值,我也不知道为什么。我检查过,仔细检查过,三次检查过代码和孤立的代码块,我认为这可能导致问题无济于事。我最近在发生问题之前所做的唯一更改就是在页面中插入了片段。

该应用程序使用旧的Todo列表访问SQL Server。我相信开放的数据库代码可能是问题所在,并已将其稍作移动以尝试缓解问题

这是我的代码

import React, { Fragment, useState } from 'react';
import { ScrollView, StyleSheet, Text, TextInput, Button,TouchableOpacity, View,Image, CheckBox, Alert } from 'react-native';
import Constants from 'expo-constants';
import * as SQLite from 'expo-sqlite';
import { enableProdMode } from '@angular/core';
import {fetch} from 'node-fetch';
let ref = React.createRef()
function performGetRequest(){
const db = SQLite.openDatabase('db.db')

  const getPage= {

  getServerPage :(
  fetch('TEST SERVER CURRENTLY DOWN')
  .then(res => res.text())
  .then(json=> console.log(json)))
  }
}
const db = SQLite.openDatabase('db.db')


  const list = {
    Page :(
     <Fragment>
       TEST
       </Fragment>
    )
  }

function Items({ done: doneHeading, onPressItem }) {
  const [items, setItems] = React.useState(null);

  React.useEffect(() => {
    db.transaction(tx => {
      tx.executeSql(
        `select * from items where done = ?;`,
        [doneHeading ? 1 : 0],
        (_, { rows: { _array } }) => setItems(_array)
      );
    });
  }, []);


  const heading = doneHeading ? "Completed" : "Todo";
    return null;
 
  return (
    <View style={styles.sectionContainer}>
      <Text style={styles.sectionHeading}>{heading}</Text>
      {items.map(({ id, done, value }) => (
        <TouchableOpacity
          key={id}
          onPress={() => onPressItem && onPressItem(id)}
          style={{
            backgroundColor: done ? "#000000" : "#fff",
            borderColor: "#000",
            borderWidth: 1,
            padding: 8
          }}

        >

          <Text style={{ color: done ? "#fff" : "#000" }}>{value}</Text>
        </TouchableOpacity>
      ))}
    </View>
  
  );}
      
        
function App() {

  const [text, setText] = React.useState(null)

  const [forceUpdate, forceUpdateId] = useForceUpdate()



  React.useEffect(() => {
    db.transaction(tx => {
      tx.executeSql(
        "create table if not exists items (id integer primary key not null, done int, value text);"
      );
    });
  }, []);



  const add = (text) => {
    // is text empty?
    if (text === null || text === "") {
      return false;
    }
  }


    db.transaction(
      tx => {
        tx.executeSql("insert into items (done, value) values (0, ?)", []);
        tx.executeSql("select * from items", [], (_, { rows }) =>
          console.log(JSON.stringify(rows))
        );
      },

      null,
      forceUpdate
    );

   let ref = React.createRef();
   const Home: React.FC = () => {
    let cancelToken: any = axios.CancelToken;
    let source = cancelToken.source();
    useEffect(() => {
        (async () => {
          try {
           const data = await axios.get("https://Kitchkar.pagekite.me/music", {
              cancelToken: source.token
          });
           
          }catch (error) {
            if (axios.isCancel(error)) {
              console.log('Request canceled', error.message);
            } else {
              // handle error
              console.log(error);
            }
          }
        })();
        return () => {
          //when the component unmounts
          console.log("component unmounted");
          // cancel the request (the message parameter is optional)
          source.cancel('Operation canceled by the user.');
        }
    }, []); //End UseEffect
  };
   return (

    <View style={styles.container}>
      <Text style={styles.heading}>Page Listing</Text>
      <View style={styles.flexRow}>
        <TextInput
          onChangeText={text => setText(text)}
          onSubmitEditing={() => {
            add(text);
            setText(null);
          }}
          
          
          placeholder="what do you need to do?"
          style={styles.input}
          value={text}


        />
      
      

      </View>
      <View style={{ width: 200}}>
      <Button
      title="Enter!"
      color="gold"
      width="100"
      height="100"
      onpress={(performGetRequest)}/>
      
      </View>
      
      <Text style={styles.heading}>server contents</Text>
      
      <Text style={styles.heading}>{getPage.getServerPage}</Text>
      <Text style={styles.heading}></Text>
      
      
<Items
          key={`forceupdate-todo-${forceUpdateId}`}
          done={false}
          onPressItem={id =>
            db.transaction(
              tx => {
                tx.executeSql(`update items set done = 1 where id = ?;`, [
                  id
                ]);
              },

              null,
              forceUpdate
            )
          }

          
        />
      <ScrollView style={styles.container}>
            <Image source={require('Substitute any image here')}/>
       <View style={{
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'flex-start',
      }}>
              
       <View style={{width: 200, height: 200, backgroundColor: 'yellow'}} />
        <View style={{width: 200, height: 200, backgroundColor: 'orange'}} />
        <View style={{width: 200, height: 200, backgroundColor: 'gold'}} />
      </View>

      <Text style={styles.heading}>Search History</Text>


        <Items
          done
          key={`forceupdate-done-${forceUpdateId}`}
          onPressItem={id =>
            db.transaction(
              tx => {
                tx.executeSql(`delete from items where id = ?;`, [id]);
              },
              null,

              forceUpdate
            )
          }
        />
<Text style={styles.heading}>Search History</Text>
      </ScrollView>
    </View>
  );
}
  

function useForceUpdate() {
  const [value, setValue] = useState(0);
  return [() => setValue(value + 1), value];
}

db.transaction(
  tx => {
    tx.executeSql("select * from items", [], (_, { rows }) =>
    tx.executeSql("insert into items (done, value) values (0, ?)", [console.log(JSON.stringify(rows))]));
  }
    );


  const styles = StyleSheet.create({
  container: {
    color:"#FFD700",
  },
  button: {
  width: "200",
  height:"100",
  },
  heading: {
    color: "#FFD700",
    fontSize: 20,
    fontWeight: "bold",
    textAlign: "center"
  },
  flexRow: {
    flexDirection: "row"
  },
  input: {
    color:"#FFD700",
    borderColor: "#FFD700",
    borderRadius: 4,
    borderWidth: 1,
    flex: 1,
    height: 48,
    margin: 16,
    padding: 8
  },
})

export default App;

1 个答案:

答案 0 :(得分:0)

const db = SQLite.openDatabase('db.db')应该是const db = SQLite.openDatabase(“ db.db”);