How to save data to database in python

How to save data to database in python – . We may have mastered the basics of python, how to process data, and so on. And on this occasion what we will discuss is how to save data into a mariadb or mysql database using the python programming language.

Before we get into practice, we need to prepare the needs first. Make sure python is installed on your computer and we need the mariadb database.

But this time we will use XAMPP for the full package. For your information XAMPP is a free and open source cross-platform web server solution package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages.

Well let’s go into the steps to install XAMP first

Download XAMPP

Install XAMPP
Here is the command to install:

$ sudo chmod +x ./

$ sudo ./

install xampp

To run the server and database we can use the following command:

$ sudo /opt/lampp/lampp start

If the error:
XAMPP: Starting Apache…/opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
Then we need to solve this netstat error by installing:

$ sudo apt-get install net-tools

Now let’s conceptualize the program that we will create. Let’s create a python program where we input the name and address, then we save it into the database in the biodata table. Let’s create a database first. Let’s name it ‘save_db’

create database

Create table ‘biodata’ with the number of columns 3

create table

Then we fill in the id, name, and address. Here we set the id as the primary key and auto increment or auto-filled

create tables (2)

After the requirements have been installed, namely python and xampp, then the database and table have been created, we will continue to discuss python code. You need to install the mysqlclient library first with the command:

$ pip3 install mysqlclient

After that let’s import and create a connection function to the database:

connection to database

Continue to make the python program like this:

full code example
import MySQLdb

def mysql_connection():
    return MySQLdb.connect(

def run():
        nama = input("masukan nama: ")
        alamat = input("masukan alamat: ")
        sql = "INSERT INTO biodata (nama, alamat) VALUES (%s, %s)"
        val = (nama, alamat)
        myConnection = mysql_connection()
        cursor = myConnection.cursor()
        cursor.execute(sql, val)
        print("data tersimpan")

    except Exception as e:
        print("data tidak tersimpan")


if __name__ == '__main__':

And let’s run the code we have written and here is the result:

run script

The essence of this discussion is that we can find out how to connect and save data to the mysql / mariadb database using the python programming language. You can find the code above by visiting the repo: . That’s all for our discussion, hopefully it will be useful, we will meet in the next post. Cheers!!

Written by:

Leave a Reply

Your email address will not be published.Required fields are marked *