#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
**************************** WARNING ***********************************************
FOR EACH PYTHON pip INSTALLATION WE DEMAND YOU NOT TO INSTALL LIBRARIES ON YOUR SYSTEM BUT UNDER VIRTUAL ENVIRONMENTS.
DOCUMENTATION CAN BE FOUND AT : 
- AREL WEBSITE : 		https://arel.eisti.fr/documents/54752
- VIRTUALENV WEBSITE : 		http://python-guide-pt-br.readthedocs.io/fr/latest/dev/virtualenvs.html
- VIRTUALENVWRAPPER WEBSITE : 	http://virtualenvwrapper.readthedocs.io/en/latest/install.html
************************************************************************************

#install mysql server
for windows install mysql-installer-community-5.7.9.0 to download from https://dev.mysql.com/downloads/installer/

for ubuntu
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
sudo apt-get update
sudo apt-get install mysql-server

mysql -u root -p


#install flask-mysqldb to connect from your program to mysql 
pip3 install flask-mysqldb #♣ pip3 install the version for python3 and pip install the version for python2.7

creation of a database and tuples

create database etudes;
use etudes;
CREATE TABLE etudiant (
    numEtudiant int PRIMARY KEY auto_increment,
    nom varchar(50) NOT NULL,
    prenom varchar(50)
) ENGINE=InnoDB;

INSERT INTO etudiant (numEtudiant, nom, prenom) VALUES (10, 'nom1', 'prenom1'); 

INSERT INTO etudiant (numEtudiant, nom, prenom) VALUES (20, 'nom2', 'prenom2'); 

select * from etudiant;


may be on ubunto we will need:
http://codeinthehole.com/tips/how-to-set-up-mysql-for-python-on-ubuntu/
sudo apt-get install python-pip
pip3 install -U pip3

sudo apt-get install python-dev libmysqlclient-dev

pip3 install MySQL-python
'''

from flask import Flask
from flask_mysqldb import MySQL
#from flask_mysqldb import MySQLdb

app = Flask(__name__)

# this section exists if we use from flask_mysqldb import MySQL and desapears if we use from flask_mysqldb import MySQLdb
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'#'db_username_here'
app.config['MYSQL_PASSWORD'] = 'eisti'#'db_password_here'
app.config['MYSQL_DB'] = 'etudes'#'database_name_here'
mysql = MySQL(app)


@app.route('/')
def etudiants():

	# these two lines exist if we use from flask_mysqldb import MySQLdb
	#db = MySQLdb.connect(host="localhost", user="root", passwd="eisti", db="etudes")
	#cur=db.cursor()

	# this line exists if we use from flask_mysqldb import MySQL
	cur = mysql.connection.cursor()

	cur.execute('''SELECT numEtudiant, nom, prenom FROM etudiant''')
	rv = cur.fetchall()

	numEtR=10  # numero d'étudiant recherché
	nomEtR='nom1' # nom d'étudiant recherché
	cur.execute("SELECT numEtudiant, nom, prenom FROM etudiant WHERE numEtudiant = %s AND nom = %s", [numEtR, nomEtR])
	row = cur.fetchone()
	if row:
		print("etudiant trouvé : %d - %s - %s\n" % (row[0], row[1], row[2]))
		# this is another equivalent manner to print a formated string
		#print("etudiant trouvé : {:d} - {:s} - {:s}\n".format(row[0], row[1], row[2])) 
	else:
		print("Etudiant non trouve.")

	return str(rv)

if __name__ == '__main__':
    app.run(debug=True)
