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.