Mariadb Connectivity With Python/Flask
Mariadb Connectivity With Python/Flask
Python/Flask
www.bispsolutions.com www.alacontec.com
Table of Contents
MariaDB connectivity with python/flask ............................................................................. Error! Bookmark not defined.
Solutions: .........................................................................................................................................................................3
Installation .......................................................................................................................... Error! Bookmark not defined.
Insert Data Page .................................................................................................................. Error! Bookmark not defined.
Steps to use flask mariadb connectivity .............................................................................. Error! Bookmark not defined.
Code to save data into Mariadb from insert data page. ....................................................... Error! Bookmark not defined.
Testing: ............................................................................................................................................................................6
Steps to test Integartion...................................................................................................................................................6
Final Result. ........................................................................................................................ Error! Bookmark not defined.
www.bispsolutions.com www.alacontec.com
MariaDB connectivity with Python/Flask
MariaDB is an open source database management system, that manage the data and the way to access to it.
It is a fork of MySQL, the main goals of this software is to be compatible with MySQL, but including new
features focused in performance. MariaDB is rapidly increasing its market share due it is totally free and
open source and its compatibility with MySQL. We chose MariaDB because is really easy to work with it if
you have worked with MySQL and because probably MariaDB will take advantage over MySQL shortly
Flask is a web framework. This means flask provides you with tools, libraries and technologies that
allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as
big as a web-based calendar application or a commercial website.
Solutions:
Note:In this document we explain step by step Integration between Python/flask and MariaDB (To
Insert data and display data into MariaDB) using MySQL connector.
Steps :
Download MariaDB
We can download MariaDB from below URL-
Link:https://downloads.mariadb.org/mariadb/10.4.7/
www.bispsolutions.com www.alacontec.com
Once its successfully downloaded installed it and after installation go to your system click on start button
and search MySQL client its look something like this.
Open it and enter the password.If your screen loos like this congratulation you have successfully connected with MariaDB
>>use bisp;
3) Now create a table named test with parameters as Name and Roll Number.
>> create table student(name varchar(20) not null,roll varchar(20) not null);
>>desc student;
www.bispsolutions.com www.alacontec.com
How to connect with Flask:
1) Install MySQL client for python by using the following command in your flask project
app = Flask(__name__)
app.config['SECRET_KEY'] = '*********'
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql://yourdatabase:yourdatabase@localhost/mysql'
db = SQLAlchemy(app)
3) For insert record in database. We are creating a html page called student.html
<!DOCTYPE html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
www.bispsolutions.com www.alacontec.com
4) Your main.py file
@app.route('/act',methods=['GET','POST'])
defact():
if (request.method == 'POST'):
try:
name = request.form['name']
roll = request.form['roll']
conn = mariadb.connect(user='your username',password='your
password',database='your database name')
cur = conn.cursor()
sql="insert into student(name,roll) values('{}','{}')".format(name,roll)
cur.execute(sql)
conn.commit()
msg="data has been stored"
return render_template('address.html',msg=msg)
except:
return "Database connection error"
Testing:
Steps to test Integartion
Step1: Start your flask app
defstudent():
cur.execute("select * from student")
rows = cur.fetchall()
return render_template('student.html',rows=rows)
www.bispsolutions.com www.alacontec.com
Edit your student.html file by entering the following code
<table border="1">
<thead>
<th>Name</th>
<th>Roll</th>
</thead>
{% for row in rows %}
<tr>
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
</tr>
{% endfor %}
</table>
Run the server and see the all records available in mariaDb
www.bispsolutions.com www.alacontec.com