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
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 Appfetch
the data from the URL (in dev mode, probably through localhost:$PORT) where your REST API is mounted.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.
The Flask version would look like:
You could use code like this after registering the Users Blueprint and creating a Model.