首页 > 解决方案 > 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;

从调试器

我不知道哪里错了,因为系统连接很容易

标签: sqlitereact-nativereact-native-android

解决方案


根据文档(react-native-sqlite-storage),预填充的文件应该在www目录下,但您共享的路径不在www目录下。同样对于预填充的数据库,openDatabase 采用以下形式的对象

{name : "testDB", createFromLocation : "~data/mioDB.db"}

data是目录的子目录www

请进行这两项更改并尝试。


推荐阅读