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
0 answers

QT QSqlDatabase open() function fails for long database name(database filepath)

QSqlError(-1, "Error opening database", "unable to open database file") when we try to open a qsqldatabase which having long database name(not supported more than 256 bytes) I tried to remove directory path from database file name and change the…
0
votes
1 answer

Connecting to an existing sqlite database to an instance of QSqlDatabase

I'm having trouble connecting an existing database to an instance of QSqlDatabase. Is it possible to do this? I've read through a good amount of posts related to this issue; most of them imply that it's at least possible to do this. However, I…
0
votes
1 answer

Binding values to stored procedure with MySQL > 5 and QSqlQuery?

I just want to run this type of procedure from Qt side: DROP PROCEDURE IF EXISTS get_patient_info_brief; DELIMITER // CREATE PROCEDURE get_patient_info_brief(IN _id int) BEGIN SELECT age, height, weight, sessions, remaining_sessions,…
IMAN4K
  • 1,265
  • 3
  • 24
  • 42
0
votes
1 answer

PyQt5 QMYSQL Driver not loaded

I know there a many questions and answers on SO for this question, but nothing worked for me. I installed the newest QT Creator 4.9.2 with Qt 5.13.0 MinGW 32-Bit and MySQLServer 5.5 to compile the QMYSQL driver. I but the driver in the sqldrivers…
crysis909
  • 25
  • 9
0
votes
0 answers

QT SQLite Can't get table info

I'm using C++ with QT 5.7.0 for processing some legacy DB (SQLite3). The DB has many tables with names 00.00.00/00:00_data_N. I can properly get data from this tables with sql query like this: SELECT * from '00.00.00/00:00_data_N' ORDER BY id And I…
murzagurskiy
  • 1,273
  • 1
  • 20
  • 44
0
votes
1 answer

erratic QSqlDatabase behavior

I open a QSqlDatabase and load the parameter with db.set[parameter Name], The connection -db.open() fail and when checking the value of db.parameter it returns an empty string. The same form used in other client programs works perfectly....sic I'm…
Erick
  • 301
  • 3
  • 12
0
votes
1 answer

Qt C++ Make QSqlDatabase static inside class

I'm trying to implement a single QSqlDatabase instance for all instances of my class. #include #include class MyClass { static QSqlDatabase db; }; QSqlDatabase MyClass::db =…
Joe Dea
  • 29
  • 2
  • 7
0
votes
4 answers

Qt - How to use SQL SELECT COUNT with parameters?

I'm writing a program in QT and have a problem with writing an SQL query Select. I have a simple table which contains columns like: ID, name_or_nickname, surname, occupation. I have 3 variables I want to use in the query: QString name =…
0
votes
0 answers

QSqlError:14 Unable to open database file

QsqlDatabasecannot open an existing file.The file is part of QT Resources. If I use file address as per resources, I get error. If I use absolute address, it works fine. I made sure file exists and path is correct. const QString…
Wafeeq
  • 869
  • 1
  • 16
  • 34
0
votes
0 answers

What happens if I don't remove database created using QSqlDatabase?

The manual describes the correct way to remove database is : { QSqlDatabase db = QSqlDatabase::addDatabase (...); QSqlQuery query (db); query.exec (...); } QSqlDatabase::removeDatabase (...); What happens if I close the database but not remove it…
Navjot Singh
  • 626
  • 1
  • 5
  • 16
0
votes
1 answer

How to cancel long-running QSqlQuery?

How to cancel long running QSqlQuery? Database is returning 3M+ rows and it's shown in QTableView control. I'd like to be able to force stop both long operations: when database is running a long operation if database is fast, but there is a huge…
mspehar
  • 527
  • 1
  • 6
  • 19
0
votes
0 answers

out of memory. Error opening database. sqlite3 and UWP kit in Qt

I made a very simple program to open a sqlite3 database and create a sample table in it. I wrote this code: #include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include…
Hemil
  • 916
  • 9
  • 27
0
votes
1 answer

Qt Threads and the default QSQLDatabase connection (sqlite)

Is it safe to use default database connection from different threads? Like this: bool upSafe(const QString &mig_to, const QString &mig_from) const { if (!QSqlDatabase::database().transaction()) { qCCritical(hfCoreMT) << "Failed init database…
ephemerr
  • 1,833
  • 19
  • 22
0
votes
0 answers

How do i fetching list of records in QCombo BOX from Sqlite3 database in PYQT5 Python using QSQL

I want to get list of records from database table. These are the values of two column name "id" and "lable" inserted into database. INSERT INTO "phone_number_type" VALUES(1,'Home'); INSERT INTO "phone_number_type" VALUES(2,'Work'); INSERT INTO…
ndj
  • 27
  • 9
0
votes
1 answer

How do I query a named database within a connected QSqlDatabase?

I have code that works: QSqlDatabase db; QSqlQuery query; db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(directory + QDir::separator() + "db.sqlite3"); db.open(); query.exec("create table mytable (id integer)"); But, if I try to name…
Kenneth Vaughn
  • 435
  • 5
  • 10