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
1
vote
3 answers

Qt5.2 can find the mysql driver but can not load it

Operating System: win7 compiler : mingw4.8(come with Qt5.2) link to : Fedora core release 5, kernel 2.6.15-1.2054_FC5 smp on an i686 db = new QSqlDatabase(); db->addDatabase("QMYSQL"); db->setHostName("localhost"); …
user3172211
  • 51
  • 1
  • 5
1
vote
2 answers

can't connect to database by executable file

i wrote a program that need to connect to database to insert some data , my executable file not connect to database but when i checked with code connection established! i don't know what is problem , do you know what is problem? i use Qt to connect…
mari
  • 417
  • 1
  • 6
  • 21
0
votes
1 answer

How to use QSqlDatabase to import database from disk to in-memory?

I want to import a database from disk to memory at application start and on exit export back from memory to disk. I converted this code from C++ to Python. It successfully exports the database from memory to disk: import sys from PySide6.QtSql…
tom
  • 1,302
  • 1
  • 16
  • 30
0
votes
0 answers

Using Qt resource paths in QSqlDatabase::setDatabaseName

I'm using Qt Creator, and I've made a "Qt Widgets Application" subproject. I'm trying to open an SQLite database that would ideally be read-only. The file is currently located in the same directory as the source code: . ├── myApp │   ├──…
Rijaja
  • 11
  • 1
  • 3
0
votes
1 answer

How to handle if database is already open in Qt?

The problem is that the database connection can be unstable, so at the beginning of the program it is easy to check this with if(!db.open()). But some cases can happen while the application is running. The point is if db.open() is false. I still…
user18197583
0
votes
0 answers

Nesting QSqlQuery queries

I am writing a Qt5/c++ app using QSqlDatabase with SQLite. I need to nest an update query inside a loop that iterates over a read query result set, as described in pseudo code here: readQuery.exec(); while(readQuery.next()) { if…
TSG
  • 4,242
  • 9
  • 61
  • 121
0
votes
0 answers

Why are the rows missing? QTableView + QSqlRelationalTableModel

I can't figure out the strange display behavior of QSqlRelationalTableModel. setEditStrategy of the model is set to OnFieldChange. I'm trying to change the values in the cells, but the whole row disappears and you can not change other cells in this…
0
votes
1 answer

QSqlDatabase connection

I have a server with a MariaDB Database. Then I have a python program who run in a client where I have this code: def Connect(): # Credential reading from register servHost = RegRead("servHost") servPort = RegRead("servPort") …
Rinfra90
  • 24
  • 6
0
votes
0 answers

QT QSQLDatabase __debugbreak() issue

I want to read sizes information from a database table and want to store them in a map. I use visual studio 2022 with qt tools addon and qt 6.4. The m_sizes map is a local variable in a configurationmanager class, the manager class lives as long as…
user2377283
  • 365
  • 1
  • 2
  • 12
0
votes
0 answers

Qt Sql Database which model need to use

I have to fetch some data from different database table and show in table view, the table view not contain all column from database but mixed from different data base table. I need to do the following. Fetch few column from table1. Fetch one cell…
NewNet
  • 5
  • 3
0
votes
0 answers

Opening Excel to read using QSqlDatabase

I can open the XLSX Excel format and read all data but only if the code for it is part of the constructor of the QMainWindow. For first tests to read the Excel it was fine. But when I move the algorithm to separate function to call it over the…
Lodhart
  • 205
  • 1
  • 6
  • 14
0
votes
1 answer

How to create a database table if the fields are declared at run time?

When creating table at run time using qsqlquery the fields of the sqlite database table are declared by user, and using that I want to create a table at run time. How can I do that in qsql cpp? qsqlQuery qry; qry.exec("CREATE TABLE xyz ....."); //…
yssh
  • 29
  • 5
0
votes
1 answer

Using database name before table name in QSQLITE C++

I am using c++ QSQLDatabase (SQLITE driver) to interact with an sqlite3 database. The c++ code uses queries that have the database's name before the table name like this : SELECT * FROM database_name.table_name ... But the query is rejected with…
Mike
  • 1
  • 1
0
votes
1 answer

Why QSqlDatabase object won't work if addDatabase method assigned later?

This won't work: { QSqlDatabase db; db.addDatabase("QSQLITE", "manDb"); QString path = QDir::currentPath()+"/"+"Sqlite.db"; db.setDatabaseName(path); if(db.open()) { qDebug()<< "Database Created successfully..."; …
yssh
  • 29
  • 5
0
votes
1 answer

Qt QPSQL QSqlDatabase: named connection fails

I am trying to have multiple QPSQL QSqlDatabase connections in my application. When I use only one connection - default/ no name, - it works flawlessly. But as soon as I try to create a named connection, it fails. The error message is: "Driver not…
Dmitriy G
  • 13
  • 1
  • 3