react-native-sqlite-storage: no such table: test - prepopulated database example

I have react-native-sqlite-storage saved/linked from master branch.

I’ve copied the example.db from PrepopulatedDatabaseExample into the correct place:

iOS: ./www/example.db

I’ve also copied the index.ios.js from PrepopulatedDatabaseExample:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

let SQLite = require('react-native-sqlite-storage')

export default class ReactNative_GrammarApp extends Component {

  constructor(props) {
    super(props)

    this.state = {
      record: null
    }

    let db = SQLite.openDatabase({name: 'test.db', createFromLocation : "~example.db", location: 'Library'}, this.openCB, this.errorCB);
    db.transaction((tx) => {
      tx.executeSql('SELECT * FROM test', [], (tx, results) => {
          console.log("Query completed");

          // Get rows with Web SQL Database spec compliance.

          var len = results.rows.length;
          for (let i = 0; i < len; i++) {
            let row = results.rows.item(i);
            console.log(`Record: ${row.name}`);
            this.setState({record: row});
          }
        });
    });

  }

  errorCB(err) {
    console.log("SQL Error: " + err);
  }

  successCB() {
    console.log("SQL executed fine");
  }

  openCB() {
    console.log("Database OPENED");
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          This is an example with sqlite3 and a prepopulated database. Enjoy!
        </Text>
        <Text style={styles.instructions}>
          {this.state.record !== null ? 'Success: ' + this.state.record.name : 'Waiting...'}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('ReactNative_GrammarApp', () => ReactNative_GrammarApp);

When I run it in the simulator it just says “Waiting…” and here is what the console output looks like:

screen shot 2017-01-30 at 4 21 54 am

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Solution:Close your editor and open again, remove the app in your decives and run again.

I had the exact same issue for the longest time - but just solved it almost inexplicably. On the open database call: let db = SQLite.openDatabase({name: 'test.db', createFromLocation : "~example.db", location: 'Library'}, this.openCB, this.errorCB); try changing the name property of your database to something else. I don’t know why, but when I started experimenting with the database name it suddenly started working. Not any arbitrary word words, but many of them do (while my database name weirdly did not). try something like this: let db = SQLite.openDatabase({name: 'a', createFromLocation : "~example.db", location: 'Library'}, this.openCB, this.errorCB);

From a glance this stmt seems incorrect:

let db = SQLite.openDatabase({name: ‘test.db’, createFromLocation : “~example.db”, location: ‘Library’}, this.openCB, this.errorCB);

Per Readme documentation:

2.SQLite.openDatabase({name : “testDB”, createFromLocation : “~data/mydbfile.sqlite”}, okCallback,errorCallback); // if your folder is called data rather than www or your filename does not match the name of the db

the correct stmt would look like this since your example.db is in www folder but it is not named the same as the database:

let db = SQLite.openDatabase({name: ‘test.db’, createFromLocation : “~www/example.db”, location: ‘Library’}, this.openCB, this.errorCB);