Я хочу добавить новые данные в существующую базу данных в 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
}
});