Site icon Infinitricks

How to save data to database in python

how to save data to database

How to save data to database in python – infinitricks.com . 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
https://www.apachefriends.org/download.html

Install XAMPP
Here is the command to install:

$ sudo chmod +x ./xampp-linux-x64-8.0.6-0-installer.run

$ sudo ./xampp-linux-x64-8.0.6-0-installer.run

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 table ‘biodata’ with the number of columns 3

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

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:

Continue to make the python program like this:

import MySQLdb

def mysql_connection():
    return MySQLdb.connect(
        host="localhost",
        user="root",
        passwd="",
        db="simpan_db"
    )

def run():
    try:
        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")
        myConnection.commit()

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

    myConnection.close()


if __name__ == '__main__':
    run()

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

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: https://github.com/pesonainformatika/save-database . That’s all for our discussion, hopefully it will be useful, we will meet in the next post. Cheers!!

Written by:
@akhisyabab

Exit mobile version