Questions tagged [qsqldatabase]

The QSqlDatabase class represents a connection to a database in Qt 5.0.

Click here for documentation

The QSqlDatabase class represents a connection to a database.

The QSqlDatabase class provides an interface for accessing a database through a connection. An instance of QSqlDatabase represents the connection. The connection provides access to the database via one of the supported database drivers, which are derived from QSqlDriver.

Alternatively, you can subclass your own database driver from QSqlDriver. See How to Write Your Own Database Driver for more information.

Create a connection (i.e., an instance of QSqlDatabase) by calling one of the static addDatabase() functions, where you specify the driver or type of driver to use (i.e., what kind of database will you access?) and a connection name. A connection is known by its own name, not by the name of the database it connects to. You can have multiple connections to one database. QSqlDatabase also supports the concept of a default connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). Subsequently, when you call any static member function that takes the connection name argument, if you don't pass the connection name argument, the default connection is assumed. The following snippet shows how to create and open a default connection to a PostgreSQL database: QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); bool ok = db.open();

Once the QSqlDatabase object has been created, set the connection parameters with setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(). Then call open() to activate the physical connection to the database. The connection is not usable until you open it.

The connection defined above will be the default connection, because we didn't give a connection name to addDatabase(). Subsequently, you can get the default connection by calling database() without the connection name argument:

QSqlDatabase db = QSqlDatabase::database(); QSqlDatabase is a value class. Changes made to a database connection via one instance of QSqlDatabase will affect other instances of QSqlDatabase that represent the same connection. Use cloneDatabase() to create an independent database connection based on an existing one.

If you create multiple database connections, specify a unique connection name for each one, when you call addDatabase(). Use database() with a connection name to get that connection. Use removeDatabase() with a connection name to remove a connection. QSqlDatabase outputs a warning if you try to remove a connection referenced by other QSqlDatabase objects. Use contains() to see if a given connection name is in the list of connections. Once a connection is established, you can call tables() to get the list of tables in the database, call primaryIndex() to get a table's primary index, and call record() to get meta-information about a table's fields (e.g., field names).

155 questions
0
votes
1 answer

Specify database path for QMYSQL

Qt 6.2.0, Ubuntu 21.10. Usually MariaDb stores database under /var/lib/mysql/ directory. I can connect to my own database using this working code: _db = QSqlDatabase::addDatabase("QMYSQL",…
Mark
  • 4,338
  • 7
  • 58
  • 120
0
votes
0 answers

Where to find PyQt5 drivers?

I can't access the SQL related drivers in PyQt5. I need said drivers to do everything. I installed PyQt5 with Anaconda I started with code to create the connection, check that the connection works, create an app for the connection con =…
0
votes
0 answers

How to save QVariantMap into QSqlDatabase

I've already got an infrastructure to work with. There is an insert function given with the following signature: insert(const QString& key, const QVariant& value) Within the function the QSqlQuery is getting prepared: QSqlQuery…
Attis
  • 573
  • 1
  • 7
  • 19
0
votes
0 answers

PyQt - "QSqlDatabase: QPSQL driver not loaded" on Windows, works on OSX

I'm trying to connect to a PostgreSQL 13 database from PyQt/PySide. When I get to con = QSqlDatabase.addDatabase('QPSQL'), I get the following console output: QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC…
Moley
  • 13
  • 1
  • 3
0
votes
2 answers

What is the correct way to open and close a database connection for a Qt worker thread

I'm working on a scenario where we want to asynchronously execute INSERT/DELETE statements on a database table (it's a fire-and-forget scenario). I'm planning to simply fire a signal with relevant data, and have the thread's event loop process each…
Tim Meyer
  • 12,210
  • 8
  • 64
  • 97
0
votes
1 answer

QSqlDatabase lastError() returns empty string for error in query

I was writing simple code using SQLite database in Qt: QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); QSqlQuery query; query.exec("CREATE TABLE IF NOT EXISTS test(id, name)"); // ... adding data here if(query.exec("SELECT * from test")) { …
max paint
  • 49
  • 6
0
votes
1 answer

PyQT5 : SpinBox values are not maintained in qsql

Work on a project and I want to make spinBox values saved in the database .. but the code does not work for me .. The result I want to get: For example, when changing the value of spinBox_1 to 5 and the value of spinBox_2 to 10, when the program is…
0
votes
0 answers

"QSqlDatabase: QPSQL driver not loaded" when trying to create a database using QtSql in Python

I am trying to create a PostgreSQL database with PyQt5. After creating a database using QSqlDatabase.addDatabase(), calling lastError().text() on that object returns the following: Driver not loaded Driver not loaded I have already tried copying…
gearmic
  • 47
  • 5
0
votes
1 answer

Create a new column with date based on 3 other columns (year, month, day)

Is there anyway to join 3 columns to create a new column with the date value I have a column name Month Day Year and I need to join them and show the date result of the values form those columns
skaailet
  • 41
  • 3
0
votes
0 answers

Should I explicitly call QSqlDatabase::removeDatabase or close?

I created db handling class with Qt's SQL : // constructor of my db handler class DBHandler::DBHandler(QObject *parent) : QObject(parent) { QSqlDatabase db = QSqlDatabase::addDatabas("QMYSQL", "mydb"); // ... initialization like setUserName(),…
Dean Lee
  • 95
  • 6
0
votes
0 answers

fast way to update 10M record in Mysql database using QT

I have a 500MB file which contains a data that I need to use it to update a table that contains 10M rows so I use this code in using QT SQL library QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); // my database info db.transaction(); //…
user7179690
  • 1,051
  • 3
  • 17
  • 40
0
votes
1 answer

How to cancel QSqldatabase::open immediately in a running thread?

I have a Thread, which opens a database and emits a signal. The main idea is to check if the database is still there and valid. If the database is offline (MYSQL), QSqldatabase is trying for about 3 seconds to connect. Now the problem: If I want to…
ratloser
  • 116
  • 1
  • 7
0
votes
2 answers

How to prevent name collisions when creating QSqlDatabase connection in multiple threads

I have multi-threaded QTcpServer and for each database request, it creates new Thread to keep server Responsive. So in each thread I have to creating new QSqlDatabase connection. But I keep getting name collisions between connections. here is my…
LightSith
  • 795
  • 12
  • 27
0
votes
1 answer

PYQT5 how to change the scheme in mysql odbc connection for QSqlTableModel?

I have a valid ODBC connection to Mysql server with root access Platform Windows 7 64bit, Python 3.6, PyQt5 The connection is to default schema 'accounts'. I want to fetch data from different schema 'acc001' I tried this but no result returned from…
user2035041
  • 69
  • 3
  • 13
0
votes
0 answers

why am i getting this qsqldatabase errors

I am having trouble while working with qsqldatabase. Here is my main code QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); …