SQlite doesn't work at all in expo

Hi there,

I need help. Have been stuck at one point since 2 days. I’m not able to create, save get data from the table using SQLite API provided by Expo. It would be a great help if anyone can help me figure out what is wrong with my project.

I have created the project using create react native boilerplate. Below is the code in my App.js

import * as Expo from "expo";
import React from 'react';
import { View, Text  } from 'react-native';

const db = Expo.SQLite.openDatabase('mydb.db');

export default class App extends React.Component {

  constructor() {
    super();
    this.state = {
      isPreparingDb: true,
    };
  }

  componentDidMount() {
     this.prepareDb();
  }

  prepareDb() {
    db.transaction((tx) => {
      tx.executeSql('drop table tabletest;');
      tx.executeSql('create table if not exists tabletest (id INTEGER PRIMARY KEY, sym text, name text);');
      console.log('table created');
    }, null, function () {
      console.log('done?.');
    });
    this.setState({ isPreparingDb: false });
  }

  render() {

    if (this.state.isPreparingDb) {
      return (
        <View>
          <Text>Loading...</Text>
        </View>
      );
    }
    
    return (
    <View>
      <Text>Loaded, data ready</Text>
    </View>
    );
  }
}

It prints “table created” on the console. It doesn’t print “done” though when I run the project via expo. Above is minimal code, in other file, i make connection again to the same db and run “SELECT * FROM tabletest”, after that I get error, “no such table: tabletest (code 1): , while compiling: SELECT * FROM tabletest”

It clearly seems that table is not created at all. What could be the cause for this API not working?

I have
node:- v8.9.1
npm:- 5.5.1
sdkVersion: 26.0.0
react: 16.3.0-alpha.1
react-native: 0.54.0

here’s an example of it in action, working on sdk26.

Thanks ccheever

It works here n snack. I even tried this example:- https://github.com/expo/sqlite-example/

This also works fine on my mobile/expo. I wonder why above code is not working. I do not see any problem with it.

Even the below simplest code is not working. And I haven’t done anything fancy, just created sample from default react native boilerplate.

const db = SQLite.openDatabase('that.db');
db.transaction((tx) => {
      tx.executeSql('drop table tabtest;');
      tx.executeSql('create table if not exists tabtest (id integer primary key, sym text, name text);', [], (tx) => {
           console.log('----created---');
      });
      tx.executeSql('SELECT * FROM tabtest;', [], (tx, results) => {
          console.log('any rows here ??');
      });
}, null, function () {
      console.log('-- are we done--?--');
 });

No console.log is ever printed in the above code. What could be the possible issue?

I think you can ignore above replies. I have found the problem

Below statement is throwing error
tx.executeSql(‘drop table tabtest;’);

and I’m not logging/catching errors in the transaction but success messages.

thanks

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.