反应本地,使用数字计数器插入数据库并显示最新值

时间:2019-11-26 23:51:21

标签: sqlite react-native

我创建了一个模块,其中有一个小的输入和计数器(加号和减号按钮),用于跟踪用户消耗的水杯。计数器工作正常,但它将值设置为state,我现在想使用expo sqlite实例。

如何更改此值,以使值等于我从数据库返回的值?换句话说,如果我选择最新的记录,我希望从记录中“浇水”来填充输入的数字,而不是将其保存在状态中

db.transaction(function(txn) {
  txn.executeSql(
    "SELECT name FROM sqlite_master WHERE type='table' AND name='waterStats'",
    [],
    function(tx, res) {
      console.log('item:', res.rows.length);
      if (res.rows.length == 0) {
        txn.executeSql(
          'CREATE TABLE IF NOT EXISTS waterStats(id INTEGER PRIMARY KEY AUTOINCREMENT, water INT(10), waterDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP)',
          []
        );
      }
    }
  );
});

 registerActivity = () => {
  var that = this;
  const { water } = this.state;
  const {navigate} = this.props.navigation;

        db.transaction(function(tx) {
          tx.executeSql(
            'INSERT INTO waterStats (water) VALUES (?)',
            [water],
            (tx, results) => {

               navigate('Dashboard');

            }
          );
        });

<View style={styles.mainContainer}>
        <View style={styles.widgetContainer}>
            <MatIcon  name="cup-water" size={50} />
            <Text style={styles.titleText}>Water</Text>
        </View>
        <View style={styles.widgetContainerButtons}>
            <NumericInput 
              value={this.state.value} 
              onChange={value => this.setState({value})}
              maxValue={8}
              onLimitReached={(isMax,msg) => console.log(isMax,msg)} 
              totalWidth={150} 
              totalHeight={50} 
              iconSize={25}
              step={1}
              valueType='real'
              rounded 
              textColor='#000' 
              iconStyle={{ color: 'white' }} 
              rightButtonBackgroundColor='#4CA1AF' 
              leftButtonBackgroundColor='#C4E0E5'/>
              <Text>Cups</Text>
        </View>
      </View>

0 个答案:

没有答案