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
18
votes
3 answers

Qt - How to bind a QList to a QSqlQuery with a "WHERE ... IN" clause?

Note: this is with SQLite, although I expect the problem is on the Qt side. First, I set up a database table from the SQLite command line tool: sqlite> create table testtable ( id INTEGER PRIMARY KEY NOT NULL, state INTEGER ); sqlite> insert into…
Travis
  • 2,961
  • 4
  • 22
  • 29
13
votes
5 answers

QPSQL driver not loaded Qt

I have some trouble when I want to add a database. _dataBase = QSqlDatabase::addDatabase("QPSQL"); After calling this method I have an error: QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC…
bin-bin
  • 532
  • 2
  • 6
  • 16
7
votes
3 answers

When or how to use fetchMore() on QSqlTableModel with a SQLite database for rowCount() to work?

My class DataTable is derived from QAbstractTableModel. It uses a QSqlTableModel object internally to fetch data from a db table. It represents a record for every row in the db (it does more but the record count is always the number of rows in the…
basic6
  • 3,643
  • 3
  • 42
  • 47
4
votes
0 answers

QtSql: Capturing Event Notification for Changes in the SQLite DataBase

I have a SQLite database. I am trying to capture events on changes in the database in a Qt program that uses QtSql module, e.g., when a new record is inserted in a table from outside the Qt program. QSqlDatabase class offers a function to subscribe…
Jatala
  • 93
  • 1
  • 11
4
votes
1 answer

QT/C++ QSqlDatabase: QMYSQL driver not loaded on OS X

I'm using OS X: 10.12.4 Qt Creator 4.0.2 MySQL 5.0.12 (looks like that, not sure) C++ Under from QT I am trying to connect to a mysql database by following code: QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("sql104.rf.gd");…
ivan8m8
  • 388
  • 4
  • 17
4
votes
2 answers

Commit changes to SQLite database, that can be seen between QTabs

I have a simple application, where I can log in / out users. When user logs in, application shows appropriate tab on main window (employee/admin/customer). I have a QMainWindow with QTabWidget on it. In my QMainWindow I create a database (I…
yak
  • 3,770
  • 19
  • 60
  • 111
4
votes
2 answers

Is there any qt signal for sql database change?

I wrote a C++ program using Qt. some variables inside my algorithm are changed outside of my program and in a web page. every time the user changes the variable values in the web page I modify a pre-created SQL database. Now I want my code to change…
user3494382
3
votes
0 answers

How do you load an SQLite3 database file into using PyQt6.QSQL?

I'm playing around with PyQt6 and QSQL. The code down here was meant to load a sqlite3 database into a QTableView But instead, it will load only the headers of a table into the table view. What do I have to correct in order to display the data along…
Gregorick
  • 61
  • 1
  • 1
  • 4
3
votes
0 answers

QSqlQuery not giving correct results when selecting TINYINT column

I have a very simple MySQL Table and want to select some of the rows using QSqlQuery. I am connected to my local development mysql server (Windows; 64-bit). When I run a SELECT query with other tools like mysql workbench, I always get the correct…
feedc0de
  • 3,646
  • 8
  • 30
  • 55
3
votes
2 answers

QSqlRelationalTableModel - two references to the same table, same foreign keys

I have 3 tables: I would like to show the Orders table (order's start and end date, user's last name, service name and service price) on GUI using QTableView and QSqlRelationalTableModel. Here's where I set up the table and the…
yak
  • 3,770
  • 19
  • 60
  • 111
3
votes
3 answers

SQL connections in several threads in a Qt application

How can I create and use several connections to a SQL db in different threads in a Qt application? I've read the documentation which says A connection can only be used from within the thread that created it. How can I separate connections in…
Hardwired
  • 804
  • 1
  • 8
  • 19
3
votes
2 answers

Showing output of sql query inside a QLabel

I am writing a qt gui application where I am planning to show the output of an sql query inside a QLabel. Now population the output inside a QTableView model is simple enough and that I can do using; QSqlDatabase dbSqlite =…
RicoRicochet
  • 2,249
  • 9
  • 28
  • 53
3
votes
2 answers

QSqlRelationalTableModel - insert record greater than 256

I have a table node={id,name}, and a table segment={id,nodeFrom,nodeTo} in a SQLite db, where node.id and segment.id are AUTOINCREMENT fields. I'm creating a QSqlTableModel for Node, as follows: nodeModel = new…
mvc
  • 653
  • 1
  • 5
  • 9
2
votes
1 answer

PyQt subclassing QSqlDatabase to make exceptions

I love all of the goodness you get from QSqlDatabase and QSqlQuery and various models. But I would really like exceptions to be thrown when errors occur. So I thought maybe I could subclass QSqlDatabase to throw an exception when the open() method…
bfris
  • 5,272
  • 1
  • 20
  • 37
2
votes
0 answers

Logging QSqlDatabase raw queries

How can I get/log all raw sql queries sent to database by QSqlDatabase? Does QSqlDatabase have any methods or events? Something like this: QSqlDatabase db = QSqlDatabase::database(connection->connectionUuid.toString()); // === SOMETHING…
Alex_Crack
  • 710
  • 1
  • 4
  • 14
1
2 3
10 11