sqlite - react-native-sqlite-storage - 没有这样的表
问题描述
有人知道连接 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;
我不知道哪里错了,因为系统连接很容易
解决方案
根据文档(react-native-sqlite-storage),预填充的文件应该在www
目录下,但您共享的路径不在www
目录下。同样对于预填充的数据库,openDatabase 采用以下形式的对象
{name : "testDB", createFromLocation : "~data/mioDB.db"}
这data
是目录的子目录www
。
请进行这两项更改并尝试。
推荐阅读
- botframework - 机器人身份验证
- python - 如何通过 Scapy 设置 TCP 选项(时间戳和 SAckOk)?
- sql - 如何对值求和并显示原始值
- lucene - 带有通配符和 NOT 的 Lucene 查询
- java - RTC + Jenkins - 仅为更改的组件构建
- output - 如何在 Informatica 中获取学生的通过/失败状态
- php - 如何使用请求字段来保存具有该值的新变量?
- ios - PKToolPicker(PencilKit)的初始位置?
- concurrency - Rust async-await:检查列表中的任何未来是否同时解析为真?
- spring-boot - Couchbase 的 Spring Data - 元数据信息