sqlite和react native expo应用程序,以文本形式插入日期
这应该是一个简单的解决办法,但我被困在这里。我有一个简单的页面,用户可以在其中输入体重,并保存到数据库中。如果我这样做只是为了插入重量,那么它就工作了。当我为列weightDate和NewestDate的值添加insert时,它就不起作用了。我创建了weightDate列文本,但我想知道问题是否在于它没有看到状态中的sqlite和react native expo应用程序,以文本形式插入日期,sqlite,react-native,expo,Sqlite,React Native,Expo,这应该是一个简单的解决办法,但我被困在这里。我有一个简单的页面,用户可以在其中输入体重,并保存到数据库中。如果我这样做只是为了插入重量,那么它就工作了。当我为列weightDate和NewestDate的值添加insert时,它就不起作用了。我创建了weightDate列文本,但我想知道问题是否在于它没有看到状态中的newestDate值来保存它 如果我删除weightDate的insert和该值,这段代码就可以正常工作。只有添加了第二个值/插入,才能使其不再工作 我做错了什么 import R
newestDate
值来保存它
如果我删除weightDate的insert和该值,这段代码就可以正常工作。只有添加了第二个值/插入,才能使其不再工作
我做错了什么
import React, { Component } from 'react';
import { StyleSheet, Text, View, Dimensions, Button,KeyboardAvoidingView, ScrollView, TouchableOpacity, TextInput, Alert} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons';
import MatIcon from '@expo/vector-icons/MaterialCommunityIcons';
import NumericInput from 'react-native-numeric-input';
import Modal from 'react-native-modal';
import moment from 'moment';
import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('testDatabase');
export default class EnterWeight extends Component{
constructor(props) {
super(props);
this.state = {
weight:'',
currentDate: new Date(),
markedDate: moment(new Date()).format("YYYY-MM-DD"),
newestDate: moment(this.currentDate).format("YYYY-MM-DD HH.MM.SS.SSS")
};
db.transaction(function(txn) {
txn.executeSql(
"SELECT name FROM sqlite_master WHERE type='table' AND name='userWeight'",
[],
function(tx, res) {
console.log('item:', res.rows.length);
if (res.rows.length == 0) {
txn.executeSql(
'CREATE TABLE IF NOT EXISTS userWeight(id INTEGER PRIMARY KEY AUTOINCREMENT, weight INT(10), weightDate TEXT',
[]
);
}
}
);
});
}
toggleWeightModal = () => {
this.setState({ isWeightModalVisible: !this.state.isWeightModalVisible });
};
registerWeight = () => {
var that = this;
const { weight } = this.state;
const { newestDate } = this.state;
const {navigate} = this.props.navigation;
db.transaction(function(tx) {
tx.executeSql(
'INSERT INTO userWeight (weight, weightDate) VALUES (?, ?)',
[weight, newestDate],
(tx, results) => {
navigate('Dashboard');
}
);
});
};
render() {
const today = this.state.currentDate;
const day = moment(today).format("dddd");
const date = moment(today).format("MMMM D, YYYY");
const newestDate = moment(today).format("YYYY-MM-DD HH.MM.SS.SSS");
return(
<View style={{ backgroundColor: 'white', flex: 1 }}>
<ScrollView keyboardShouldPersistTaps="handled">
<KeyboardAvoidingView
behavior="padding"
style={{ flex: 1, justifyContent: 'space-between' }}>
<TextInput
placeholder="Enter weight"
onChangeText={weight => this.setState({ weight })}
maxLength={10}
keyboardType="numeric"
style={{ padding:10 }}
/>
<Text>{newestDate}</Text>
<TouchableOpacity onPress={this.registerWeight.bind(this)}>
<Text>Save Entry</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
</ScrollView>
</View>
);
}
}
import React,{Component}来自'React';
从“react native”导入{样式表、文本、视图、尺寸、按钮、键盘AvoidingView、ScrollView、TouchableOpacity、TextInput、Alert};
从“@expo/vector icons/Ionicons”导入图标;
从“@expo/vector icons/materialcommunitycons”导入MatIcon;
从“反应本机数值输入”导入数值输入;
从“反应本机模态”导入模态;
从“力矩”中导入力矩;
从“expo SQLite”导入*作为SQLite;
const db=SQLite.openDatabase('testDatabase');
导出默认类EnterWeight扩展组件{
建造师(道具){
超级(道具);
此.state={
重量:'',
currentDate:新日期(),
markedDate:moment(new Date())。格式(“YYYY-MM-DD”),
最新日期:时刻(this.currentDate).format(“YYYY-MM-DD HH.MM.SS.SSS”)
};
数据库事务(功能(txn){
txn.executeSql(
“从sqlite_master中选择名称,其中type='table'和name='userWeight'”,
[],
功能(发送、恢复){
log('item:',res.rows.length);
if(res.rows.length==0){
txn.executeSql(
'如果不存在,则创建表userWeight(id INTEGER主键自动递增,weight INT(10),weightDate文本',
[]
);
}
}
);
});
}
toggleWeightModal=()=>{
this.setState({isWeightModalVisible:!this.state.isWeightModalVisible});
};
注册表重量=()=>{
var=这个;
const{weight}=this.state;
const{newestDate}=this.state;
const{navigate}=this.props.navigation;
数据库事务(功能(tx){
tx.executeSql(
'插入用户权重(权重、权重日期)值(?,)',
[重量,最新日期],
(发送,结果)=>{
导航(“仪表板”);
}
);
});
};
render(){
const today=this.state.currentDate;
常数日=时刻(今天)。格式(“dddd”);
const date=时刻(今天)。格式(“MMMM D,YYYY”);
const newestDate=时刻(今天)。格式(“YYYY-MM-DD HH.MM.SS.SSS”);
返回(
this.setState({weight})}
maxLength={10}
keyboardType=“数字”
样式={{填充:10}}
/>
{newestDate}
保存条目
);
}
}
什么不起作用?什么不起作用?