skip to Main Content

I developed a simple app using React Native and I am trying to connect to a database. I looked online and I found out that many companies like Facebook, Instagram, YouTube and many more are using MySQL + Python.

I want to use Python to manipulate (insert/delete and update) the database but in order to do that I need to create an API. Using NodeJS + Express I would do something like this:

    import express from 'express';
import mysql from 'mysql';

const app = express();

app.listen(port, ()=> {
  console.log('server started');
  // do stuff
});


//database
const database = mysql.createConnection({
  host : 'localhost',
  user : 'root',
  password : 'password12345',
  database : 'database_app'
});

//create 
app.get('/create', (req, res)=> {
  // check error
  // create query
});

//delete 
app.get('/delete', (req, res)=> {
  // check error
  // delete query
});

//update 
app.get('/update', (req, res)=> {
  // check error
  // update query
});

How do I do the same with Python? and how do I connect it to my app?

2

Answers


  1. First of all I don’t have a strong python background but I know that people use flask for building REST APIs and the way you can connect the API to your app is via URL, you just need to start your server and in the client side of your RN App fetch the data from the URL (in dev mode, probably through localhost:$PORT) where your REST API is mounted.

    Login or Signup to reply.
  2. You’ll need something like Django or Flask to serve up the data for it to be consumed by the front end.

    In Flask, you make a blueprint, then use that blueprint to serve routes, which is analogous to what you were doing in your Node.js code.

    app.get('/update', (req, res)=> {
      // check error
      // update query
    });
    

    The Flask version would look like:

    users_blueprint = Blueprint('users', __name__, template_folder='./templates')
    
    
    @users_blueprint.route('/users/', methods={'GET'})
    def getUsers():
      return jsonify({
        'status': 'success',
        'message': 'test response msg',
        'data' : mydata
      })
    

    You could use code like this after registering the Users Blueprint and creating a Model.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search