我正在使用这个库:https : //github.com/andpor/react-native-sqlite-storage用于我的本机项目。
我像这样从数据库中检索单行:
db.transaction((tx) => {
tx.executeSql('SELECT * FROM user where id = 1', [], (tx, results) => {
console.log("Query completed");
var len = results.rows.length;
if (len > 0) {
let row = results.rows.item(0);
this.setState({userName: row.name});
}
});
});
return (
<View>
<Text>{this.state.userName}</Text>
</View>
)
但现在我想从数据库中获取所有数据,告诉我我在下面的代码中做错了什么:
db.transaction((tx) => {
tx.executeSql('SELECT * FROM user', [], (tx, results) => {
console.log("Query completed");
var len = results.rows.length;
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
this.setState({record: row.name});
}
});
});
return (
<View>
<FlatList data={this.state.record}
keyExtractor={(x,i) => 1}
renderItem={ ({item}) =>
<ListItem><Text>{item.name}</Text></ListItem>
}
/>
</View>
)
谁能指导我如何在列表视图中显示数据库数据。