PySide6 Database CRUD Operations
Course Title: PySide6 Application Development Section Title: Integrating Databases with PySide6 Topic: Performing CRUD (Create, Read, Update, Delete) operations
Overview
In this topic, you will learn how to perform basic database operations using QSqlDatabase and QSqlQuery in PySide6. We'll cover how to create, read, update, and delete database records. You'll understand how to use these essential concepts to build a robust and interactive PySide6 application.
Performing CRUD Operations
CRUD operations are fundamental to any database-driven application. These operations enable you to interact with the database by creating new records, reading existing ones, updating existing records, and deleting unnecessary records.
Create Operation
To perform a create operation in PySide6, you can use the QSqlQuery
class to execute an SQL INSERT INTO
query. Here's an example:
from PySide6.QtSql import QSqlDatabase, QSqlQuery
# Connect to the database
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('database.db')
db.open()
# Create a QSqlQuery object
query = QSqlQuery(db)
# Prepare the INSERT INTO query
query.prepare('INSERT INTO table_name (column1, column2) VALUES (?, ?)')
# Bind the values to the placeholders
query.bindValue(0, 'Value 1')
query.bindValue(1, 'Value 2')
# Execute the query
query.exec()
# Close the database connection
db.close()
This code inserts a new record into the table_name
table with the values Value 1
and Value 2
.
Read Operation
To perform a read operation in PySide6, you can use the QSqlQuery
class to execute an SQL SELECT
query. Here's an example:
from PySide6.QtSql import QSqlDatabase, QSqlQuery
# Connect to the database
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('database.db')
db.open()
# Create a QSqlQuery object
query = QSqlQuery(db)
# Prepare the SELECT query
query.prepare('SELECT * FROM table_name WHERE column1 = ?')
# Bind the value to the placeholder
query.bindValue(0, 'Value 1')
# Execute the query
query.exec()
# Fetch the result
while query.next():
print(query.value(0), query.value(1))
# Close the database connection
db.close()
This code selects all records from the table_name
table where column1
equals Value 1
and prints the results.
Update Operation
To perform an update operation in PySide6, you can use the QSqlQuery
class to execute an SQL UPDATE
query. Here's an example:
from PySide6.QtSql import QSqlDatabase, QSqlQuery
# Connect to the database
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('database.db')
db.open()
# Create a QSqlQuery object
query = QSqlQuery(db)
# Prepare the UPDATE query
query.prepare('UPDATE table_name SET column2 = ? WHERE column1 = ?')
# Bind the values to the placeholders
query.bindValue(0, 'New Value 2')
query.bindValue(1, 'Value 1')
# Execute the query
query.exec()
# Close the database connection
db.close()
This code updates the column2
value to New Value 2
where column1
equals Value 1
in the table_name
table.
Delete Operation
To perform a delete operation in PySide6, you can use the QSqlQuery
class to execute an SQL DELETE
query. Here's an example:
from PySide6.QtSql import QSqlDatabase, QSqlQuery
# Connect to the database
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('database.db')
db.open()
# Create a QSqlQuery object
query = QSqlQuery(db)
# Prepare the DELETE query
query.prepare('DELETE FROM table_name WHERE column1 = ?')
# Bind the value to the placeholder
query.bindValue(0, 'Value 1')
# Execute the query
query.exec()
# Close the database connection
db.close()
This code deletes all records from the table_name
table where column1
equals Value 1
.
Practical Takeaways
- Use
QSqlQuery
to execute SQL queries. - Use
QSqlDatabase
to connect to a database. - Use prepared statements to prevent SQL injection.
- Use parameter binding to replace placeholders in queries.
Conclusion
In this topic, we covered performing CRUD operations in PySide6. You learned how to create, read, update, and delete database records using the QSqlQuery
class. You also learned how to use prepared statements and parameter binding to make your database interactions safer and more efficient.
What's Next
In the next topic, you'll learn how to display database data in views using QTableView
.
If you have any questions or need help, leave a comment below.
Images

Comments