1

This is a pretty basic React Native app I am making with Expo CLI. I already have a populated SQL Database called (I know this is a bad name) pythonsqlite.db with a table Users that I am trying to query using react-native-sqlite-storage.

One of my screens has a button with an onPress function that querys the database for all users:

import React, {useState} from 'react';
import { Image, StyleSheet, Text, View, TouchableOpacity, TextInput, Button, Picker } from 'react-native';
import { NavigationContainer, useNavigation, useRoute } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
//import { openDatabase } from "expo-sqlite";
import { openDatabase } from 'react-native-sqlite-storage';
import SQLite from 'react-native-sqlite-storage';
//import * as SQLite from 'expo-sqlite';
//import * as FileSystem from "expo-file-system";
//import {Asset} from "expo-asset";


const CharacterCreateScreen = ({navigation, route}) =>{
  return(
    <View style={styles2.container}>
      <Text>Sad :(</Text>
      <TouchableOpacity
          onPress={() => dbCall()}
          style={{ backgroundColor: 'red', marginRight:30 }}>
          <Text>Button</Text>
        </TouchableOpacity>
  </View>
  )
}


  function dbCall(){
  function errorCB(err) {
      console.log("SQL Error: " + err);
    }
    
    function successCB() {
      console.log("SQL executed fine");
    }
    
    function openCB() {
      console.log("Database OPENED");
    }


   var db = SQLite.openDatabase("pythonsqlite.db", "1.0", "Test Database", 200000, openCB, errorCB);

    var userQuery = "SELECT * FROM Users";

    db.transaction((tx) => {
      tx.executeSql(userQuery, [], (error, results) => { //callback function to handle the results
        alert("Query Completed.");
        //alert(results);
        // Get rows with Web SQL Database spec compliance.
        var len = results.rows.length;
        alert(len);
        for (let i = 0; i < len; i++) {
          let row = results.rows.item(i);
          arr1.push(results.rows.item(i));
          alert(`User name: ${row.userName}`);
        }
      });
    }); //alert("Done.");
  }

However upon pressing the button I get this error message on my IOS device: enter image description here

I tried running this command as is stated on this page:

cd ios && pod install && cd ..

I bash I got this error:

bash: cd: ios: No such file or directory

In Windows Powershell:

The token '&&' is not a valid statement separator in this version.

This is where I am stuck. I'm not exactly sure where to go. This is very similar to this question, but it is 3 years old and doesn't seem to be answered. I've been trying to figure this out for a really long time if anyone could help me that would be very appreciated.

3 Answers 3

0
import { SQLite } from 'react-native-sqlite-storage';

should be

import SQLite from 'react-native-sqlite-storage';

SQLite is exported as a default export ... not a named export.

Edit

The new error means the module is not linked properly...

If you're using ReactNative 0.60 and above Run cd ios && pod install && cd .. ... Linking is not required in React Native 0.60 and above

if lower than that ... you'll have to follow the steps in this link

Sign up to request clarification or add additional context in comments.

3 Comments

This is interesting. I will try this when I get home from work and let you know what happens.
This changed the error message but did not fix the problem. I have updated the question to reflect the new error message although it is very similar.
Using React Native version 0.62.2. In bash running the above command returns ios no such file or directory. Windows PowerShell returns && is not a valid operator. What should I do?
0

You're using the managed workflow in expo, right? If so, you should be following the example here and using the expo-sqlite package and not the react-native-sqlite-storage directly.

import * as SQLite from "expo-sqlite";
// later in the file 
var db = SQLite.openDatabase("pythonsqlite.db", /* other args */);

If you're using the bare workflow, or this still doesn't work, then I'll have to take a closer look.

Comments

0

So easy (in my lucky case!)..., changing:

import { SQLite } from "expo-sqlite";

to

import * as SQLite from "expo-sqlite";

fixed the error for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.