How can I access data in a database on a hosted website using a python application?

Yedenn

The project I'm working on is in two parts. The first is a website, written in native HTML/CSS/PHP/JS, with a MySQL database.

For the second part, I've been asked to create a python application that should, among other things, retrieve certain data from the database, store it on the machine and then delete it from the site. The thing is, I have absolutely no clue how to get my application to access the online database, or what exactly to look for.

I believe that the people in charge of the project would like to host the site with OVH's cheapest plan, but I don't know if that's compatible.

The Coding Penguin

MySQL connectors

There are several connectors allowing to interact with an SQL database within a Python script, here is some documentation about one working with MySQL:

They roughly all work the same way:

  1. Create the connection and assign it to a variable ;
  2. Execute commands through the connection and get the value returned (which may be some data you requested or a code meaning the instruction you sent has been taken into account) ;
  3. Close the connection once you are done using it.

Compatibility with a remote OVH server

It does not matter where the database is hosted, actually, as long as it is reachable by the machine you are running the script on. Make sure it is (one way would be allowing the MySQL port - 3306 by default - to be reached on its public IP, but there are many other ways to do so).

Examples

Here are some examples from the doc I linked.

Select all employees hired in the year 1999 and print their names and hire dates to the console

import datetime
import mysql.connector

cnx = mysql.connector.connect(user='scott', database='employees')
cursor = cnx.cursor()

query = ("SELECT first_name, last_name, hire_date FROM employees "
         "WHERE hire_date BETWEEN %s AND %s")

hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31)

cursor.execute(query, (hire_start, hire_end))

for (first_name, last_name, hire_date) in cursor:
  print("{}, {} was hired on {:%d %b %Y}".format(
    last_name, first_name, hire_date))

cursor.close()
cnx.close()

Use a database or create it if it does not exist

def create_database(cursor):
    try:
        cursor.execute(
            "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
    except mysql.connector.Error as err:
        print("Failed creating database: {}".format(err))
        exit(1)

try:
    cursor.execute("USE {}".format(DB_NAME))
except mysql.connector.Error as err:
    print("Database {} does not exists.".format(DB_NAME))
    if err.errno == errorcode.ER_BAD_DB_ERROR:
        create_database(cursor)
        print("Database {} created successfully.".format(DB_NAME))
        cnx.database = DB_NAME
    else:
        print(err)
        exit(1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I grant AWS users to access website hosted by s3 bucket?

how can i access a website that is hosted inside qemu from the host operating system?

Website is hosted, I can access it, but my friend cannot reach it?

Can I use Service Principal to authenticate an application that is not hosted in Azure using role-based access control?

How can I access windows services or data from windows machine from my website using PHP?

How can i access a database hosted on bluehost/godaddy/hostinger from xampp installation

How can I send data from a Node.js application hosted on Heroku to a PHP file hosted on a completely separate (Cpanel) server?

How can I show a website to other people if it's hosted locally?

Python-Selenium : How can I get access to this part of the website?

How can I connect to a Oracle Database using Entity Framework and Oracle Managed Data Access?

How can I loop scraping data for multiple pages in a website using python and beautifulsoup4

How can I save Website data to file using VBScript?

How can I use a variable in website link using Python?

How can I open a website in my web browser using Python?

How can I take a screenshot/image of a website using Python?

How can I parse a website using Selenium and Beautifulsoup in python?

How can I parse a website using Selenium and Beautifulsoup in python?

How can I log in this specific website using Python Requests?

How do I output database data to website?

How can i go to my website without typing "https://" in url. My website is hosted on aws cloudfront

How do I access files hosted on web via python?

can I automatically access a certain table in a website and copy the data into mine using Asp.net ?

How can I save data json in database using cast on the laravel?

How can i Insert data to database using php and codeigniter

How can i save a data on database using django views?

How can I retrieve Data from a database by using the URL

Can't access a database from a WCF Data Service hosted in an ASP NET App

Can I build a basic website to access my resume but slowly customize it using python to learn and showcase my skills?

how can i access json data using for loop in vue js