如何在已创建的React Native数据库中添加注册表数据

时间:2018-08-30 05:19:24

标签: reactjs react-native native

我想在React Native的现有数据库中添加新数据,但是我无法上传数据库中的数据。

这是我的代码。我是React Native的新手。

db.transaction((tx) => {
          var sql = 'INSERT * into user WHERE values(\'' + this.state.name + '\', \'' + this.state.username + '\', \'' + this.state.password + '\')';

我对此行有疑问,请检查我的完整代码

 import React, { Component } from 'react';

 import { StyleSheet, View, TextInput, Button, Text, Alert, AsyncStorage } from 'react-native';


    var SQLite = require('react-native-sqlite-storage')`enter code here`
    var db = SQLite.openDatabase({name: 'test1.db', createFromLocation: '~test.db'}, this.openCB, this.errorCB) 

    export default class Signup extends Component {

    constructor() {

        super()

        this.state = {

              name: '',
          username: '',
          password: ''

        };
      }

      errorCB(err){
        ToastAndroid.show("SQL Error: " + err, ToastAndroid.SHORT);
      }

      successCB(){
        ToastAndroid.show("SQL executed fine", ToastAndroid.SHORT);
      }

      openCB(){
        console.log("Open database");
      }


      UserRegistrationFunction = () =>{

        db.transaction((tx) => {
          var sql = 'INSERT * into user WHERE values(\'' + this.state.name + '\', \'' + this.state.username + '\', \'' + this.state.password + '\')';



          tx.executeSql(sql, [], (tx, results) => {

            var len = results.rows.length;
            if(len == 0)
            ToastAndroid.show('Please Enter Details', ToastAndroid.SHORT);



           }
        );
      });
    }

     render() {
        return (

    <View style={styles.MainContainer}>

            <Text style= {styles.title}>User Registration Form</Text>

            <TextInput
              placeholder="Enter User Name"
              onChangeText={name => this.setState({name : name})}
              underlineColorAndroid='transparent'
              style={styles.TextInputStyleClass}
              />

            <TextInput
              placeholder="Enter User Email"
              onChangeText={email => this.setState({email : email})}
              underlineColorAndroid='transparent'
              style={styles.TextInputStyleClass}
              />

            <TextInput
              placeholder="Enter User Password"
              onChangeText={password => this.setState({password : password})}
              underlineColorAndroid='transparent'
              style={styles.TextInputStyleClass}
              secureTextEntry={true}
              />

            <Button title="Click Here To Register" onPress={this.UserRegistrationFunction} color="#2196F3" />



    </View>

        );
      }
    }

    const styles = StyleSheet.create({

    MainContainer :{

      justifyContent: 'center',
      flex:1,
      margin: 10
    },

    TextInputStyleClass: {

      textAlign: 'center',
      marginBottom: 7,
      height: 40,
      borderWidth: 1,
      borderColor: '#2196F3',
      borderRadius: 5 ,
    },

    title:{

      fontSize: 22, 
      color: "#009688", 
      textAlign: 'center', 
      marginBottom: 15
    }

    });

0 个答案:

没有答案