0

I am a completely newbie in react native and i am trying to learn it. I want to get results from SQLite and pop them in the array which i could then use in Flat list to display .. Here is my Code:

    //imports

import React,{Component} from 'react';
import {View,Text,FlatList} from 'react-native';



//create component

var SQLite = require('react-native-sqlite-storage')
var db=SQLite.openDatabase({name: 'test.db', createFromLocation: '~sqliteexample.db'})

class DbTask extends Component {


    constructor(props){
        super(props)

        this.state={
            data: []

        };

        db.transaction((tx)=>{

            tx.executeSql('Select * from `pet`',[],(tx,results)=>{

               var len=results.rows.length;
                for(let i=0; i<len; i++){
                 var row=results.rows.item(i);
                 this.state.data.push(row.owner);

                 }


            });

        });

    };


    render(){

        return(
        <View>

           <FlatList data={this.state.data} 
           renderItems={
            ({item})=> <Text>{item.owner}</Text>

           }
           > </FlatList>
        </View>
        );

     }

}


export default DbTask;

I tried to add some alerts in For loop and it showed me correct names of the owners . But i don't get it , either i am not able to push them in the "data" array or my Flat list isn't working.

Any help would be appreciated. Thanks in advance. Regards.

1 Answer 1

1

P.S : I modified my database a little so other than that the problem was in my flat-List. Just modified the flat-List like below and it worked like a charm:

<FlatList 
    data={this.state.data} 
    keyExtractor={((item, index) => "itemNo-" + index)}
    renderItem={({item}) => (
        <View style={{flex:1 , flexDirection:'row',justifyContent:'flex-start',marginBottom:10}}>
            <View style={{paddingLeft:20,width:60}}>
            <Text>{item.Number}</Text>
            </View>
            <View style={{paddingLeft:40,width:120}}>
            <Text>{item.Owner}</Text>
            </View>
            <View style={{paddingLeft:50,width:150}}>
            <Text>{item.PetName}</Text>
            </View>
        </View>
    )
  }
/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank this. work for me. but unfortunately i cannot map the array. when i map an array it says undefined. is there any solutions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.