Wednesday, December 2, 2020

Real Python: Handling SQL Databases With PyQt: The Basics

Building applications that use an SQL database is a fairly common programming task. SQL databases are everywhere and have great support in Python. In GUI programming, PyQt provides robust and cross-platform SQL database support that allows you to create, connect to, and manage your databases consistently.

PyQt’s SQL support fully integrates with its Model-View architecture to help you in the process of building database applications.

In this tutorial, you’ll learn how to:

  • Use PyQt’s SQL support to reliably connect to a database
  • Execute SQL queries on a database using PyQt
  • Use PyQt’s Model-View architecture in database applications
  • Display and edit data using different PyQt widgets

The examples in this tutorial require a basic knowledge of the SQL language, especially of the SQLite database management system. Some previous knowledge of GUI programming with Python and PyQt will also be helpful.

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Connecting PyQt to an SQL Database

Connecting an application to a relational database and getting the application to create, read, update, and delete the data stored in that database is a common task in programming. Relational databases are generally organized into a set of tables, or relations. A given row in a table is referred to as a record or tuple, and a column is referred to as an attribute.

Note: The term field is commonly used to identify a single piece of data stored in a cell of a given record in a table. On the other hand, the term field name is used to identify the name of a column.

Each column stores a specific kind of information, such as a names, dates, or numbers. Each row represents a set of closely related data, and every row has the same general structure. For example, in a database that stores data about the employees in a company, a specific row represents an individual employee.

Most relational database systems use SQL (structured query language) for querying, manipulating, and maintaining the data held in the database. SQL is a declarative and domain-specific programming language specially designed for communicating with databases.

Relational database systems and SQL are widely used nowadays. You’ll find several different database management systems, such as SQLite, PostgreSQL, MySQL, MariaDB, and many others. You can connect Python to any of these database systems using a dedicated Python SQL library.

Note: Even though PyQt’s built-in SQL support is the preferred option for managing SQL databases in PyQt, you can also use any other library to handle the database connection. Some of these libraries include SQLAlchemy, pandas, SQLite, and so on.

However, using a different library to manage your databases has some drawbacks. You won’t be able to take advantage of the integration between PyQt’s SQL classes and the Model-View architecture. In addition, you’ll be adding extra dependencies to your application.

When it comes to GUI programming with Python and PyQt, PyQt provides a robust set of classes for working with SQL databases. This set of classes will be your best ally when you need to connect your application to an SQL database.

Note: Unfortunately, PyQt5’s official documentation has some incomplete sections. To work around this, you can check out the PyQt4 documentation, the Qt For Python’s documentation, or the original Qt documentation. In this tutorial, some links take you to the original Qt documentation, which is a better source of information in most cases.

In this tutorial, you’ll learn the basics of how to use PyQt’s SQL support to create GUI applications that reliably interact with relational databases to read, write, delete, and display data.

Creating a Database Connection

Connecting your applications to a physical SQL database is an important step in the process of developing database applications with PyQt. To perform this step successfully, you need some general information about how your database is set up.

For example, you need to know what database management system your database is built on, and you might also need to have a username, a password, a hostname, and so on.

In this tutorial, you’ll use SQLite 3, which is a well-tested database system with support on all platforms and minimal configuration requirements. SQLite allows you to read and write directly to databases in your local disk without the need for a separate server process. That makes it a user-friendly option for learning database application development.

Another advantage of using SQLite is that the library comes shipped with Python and also with PyQt, so you don’t need to install anything else to start working with it.

In PyQt, you can create a database connection by using the QSqlDatabase class. This class represents a connection and provides an interface for accessing the database. To create a connection, just call .addDatabase() on QSqlDatabase. This static method takes an SQL driver and an optional connection name as arguments and returns a database connection:

QSqlDatabase.addDatabase(
    driver, connectionName=QSqlDatabase.defaultConnection
)

The first argument, driver, is a required argument that holds a string containing the name of a PyQt-supported SQL driver. The second argument, connectionName, is an optional argument that holds a string with the name of the connection. connectionName defaults to QSqlDatabase.defaultConnection, which normally holds the string "qt_sql_default_connection".

If you already have a connection called connectionName, then that connection is removed and replaced with a new connection, and .addDatabase() returns the newly added database connection back to the caller.

Read the full article at https://realpython.com/python-pyqt-database/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...