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.
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:
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).
Here are some examples from the doc I linked.
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()
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)