有人知道连接react-native-sqlite-storage的最佳方式吗?我的调试器在查询后得到“没有这样的表: nameOfTable (代码1)”。
我应该从一个名为"person“的表中获取"id”、"name“和"last name”。我已经连接了位于"/android/app/src/main/assets/mioDB.db“中的预填充数据库("mioDB")。
这是我的小代码:
import React,{Component} from 'react';
import {TextInput,View,Text,TouchableOpacity} from 'react-native';
import { openDatabase } from "react-native-sqlite-storage";
let mioDB = openDatabase('mioDB');
class App extends Component{
constructor(props){
super(props);
this.state={
name:'',
lastName:''
};
}
showData=()=>{
alert('name: '+this.state.name+' lastName: '+this.state.lastName);
};
listOfNames=()=>{
try{
mioDB.transaction((statement)=>{
console.log('****************');
statement.executeSql('SELECT * FROM person',[],(statement,results)=>{
console.log('****************');
var len = results.rows.length;
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
console.log(`Record: ${row.name}`);
}
});
});
}catch(error){
alert(error);
}
};
render(){
return(
<View style={{marginTop:100}}>
<TextInput style={{fontSize:20}} placeholder='name' onChangeText={(text)=>this.setState({name:text})}/>
<TextInput style={{fontSize:20}} placeholder='lastName' onChangeText={(text)=>this.setState({lastName:text})}/>
<TouchableOpacity style={{width:150,height:50}} onPress={this.showData}><Text>Create db</Text></TouchableOpacity>
<TouchableOpacity style={{width:150,height:50}} onPress={this.listOfNames}><Text>Show database</Text></TouchableOpacity>
</View>
);
}
}
export default App;我不知道哪里错了,因为连接系统很容易
发布于 2018-05-23 19:45:56
根据文档(react-native-sqlite-storage),预先填充的文件应该在www目录下,但是您共享的路径不在www目录下。此外,对于预填充的数据库,openDatabase采用以下形式的对象
{name : "testDB", createFromLocation : "~data/mioDB.db"}其中,data是www目录的子目录。
请进行这两项更改并尝试。
https://stackoverflow.com/questions/50440239
复制相似问题