1

I'm using React Native. I want to find the data I entered in React Native in the database. For example, in the database of the user name I entered, "select id from table where ('data I entered in react native')". I want to find the table with the user name and pull the user's id.

var name = this.state.username;

"select id from table where (name)"

I want to pull the id of the user name like this.

2
  • Visit it dev.to/saulojoab/… Commented Oct 31, 2019 at 7:38
  • Use this format to add variables in the string select id from table where ${name} Commented May 23, 2021 at 11:32

2 Answers 2

3

There is no direct connection between RN and Mysql. Use Node js for this.

Step: 1

npm install express

npm install body-parser

npm install mysql

Step: 2

const connection = mysql.createPool({
  host     : 'localhost', // Your connection adress (localhost).
  user     : 'root',     // Your database's username.
  password : '',        // Your database's password.
  database : 'my_db'   // Your database's name.
});

// Starting our app.
const app = express();

// Creating a GET route that returns data from the 'users' table.
app.get('/users', function (req, res) {
    // Connecting to the database.
    connection.getConnection(function (err, connection) {

    // Executing the MySQL query (select all data from the 'users' table).
    connection.query('SELECT * FROM users', function (error, results, fields) {
      // If some error occurs, we throw an error.
      if (error) throw error;

      // Getting the 'response' from the database and sending it to our route. This is were the data is.
      res.send(results)
    });
  });
});

// Starting our server.
app.listen(3000, () => {
 console.log('Go to http://localhost:3000/users so you can see the data.');
});

Now, how do we get that data on our React Native App?

That's simple, we use the fetch function. To do that, instead of using 'localhost:3000', you'll have to directly insert your PC's ip adress. If you use 'localhost', you're acessing your smartphone/emulator's localhost. And that's not what we want. Follow this example:

test(){
    fetch('http://yourPCip:3000/users')
      .then(response => response.json())
      .then(users => console.warn(users))
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Hi! Would you be able to explain how to use "users" variable to get the data? Like what would be an example of code to do this? That's what confused me, if you could clear that I would very much appreciate it! Thank you so much
The app.get('/users', etc) block is what happens at /users. You can add another block in the same way, e.g. app.get('/names', etc)
Thanks for the answer. But is this method is used in production app and is it scalable method ? @Murat
1

You need to have a backend service/API in order to fetch data from database. try using Node, and write a simple backend since its JavaScript. You can execute sql queries on backend, retrive data from mySQL to your node server and then you can fetch data from the backend server to react-native using fetch method. (both your backend API and the device that running react native application should be running on the same network.)

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.