pythondatabasemysql-connector-python

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


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.


Solution

  • 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)