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
1 answer

How to upload an image to SQLite Database?

I am trying to create an application where the user uploads a picture and it gets saved in SQLite Database. So far i have successfully managed to upload the image to the application while following a tutorial , however i am not sure what to do to…
nanay
  • 301
  • 1
  • 3
  • 9
1
vote
0 answers

Access denied for user during SSL Connection to AWS RDS using MySQL Connector/C++

I am trying to make a secure SSL connection to my Amazon Web Service RDS instsance. A user 'ericencrypt' has been created with privileges as follows: CREATE USER 'ericencrypt'@'%' identified by 'xxxxxx'; GRANT SELECT ON table1.* TO 'ericencrypt'@'%'…
Eric Lui
  • 11
  • 3
1
vote
1 answer

QSqlError("", "Parameter count mismatch", "")

Sorry for my English. I'm trying to select some data from a database table in order to insert the into a table from another database. This is my code: void Partes::Guardar_Solicitud() { hospital=QSqlDatabase::addDatabase("QSQLITE"); …
Alberto7
  • 51
  • 1
  • 7
1
vote
1 answer

QSqlDatabase / QSqlQuery terminating running query?

I'm using QSqlDatabase and am trying to find a way to kill a running query (kind of a necessary feature of the sql client I'm writing) Is there a way to get the id for a running query? (the plan is to just execute a kill/pg_cancel_backend command in…
Chelsea Urquhart
  • 1,388
  • 1
  • 11
  • 18
1
vote
3 answers

C# SQL table select and count distinct values

i have ASP website with SQL database table. in this table column mane "type". i want to get all distinct values from this column with values count in datatable. for example for database table: id type --------- 1 type1 2 type2 3 type3 4 …
user281812
  • 195
  • 3
  • 14
1
vote
1 answer

QSqlDatabase delete all rows from table

Hi I need to delete all rows from QSqlDatabase table, my code looks, QString dbName = QDir::currentPath()+"/DB"; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(dbName); if(db.open()){ …
Haris
  • 13,645
  • 12
  • 90
  • 121
1
vote
1 answer

Make a relation to a relation to a column in QSql

I am trying to display an QSqlRelationalTableModel with a relation to a relation to a column. This is my minimal code: from PyQt5.QtSql import QSqlDatabase, QSqlRelationalTableModel, QSqlRelation from PyQt5.QtWidgets import QApplication,…
sonovice
  • 813
  • 2
  • 13
  • 27
1
vote
2 answers

Qt: Writing to a database from multiple threads using one connection

Here's the problem: let's say we have a lot of threads (>1000) receiving data via some protocol and I want to write this data in database (sql server). For database connection, we use QSqlDatabase. According to the documentation of QSqlDatabase: A…
1
vote
2 answers

QSqlRelationalTableModel with QSqlRelationalDelegate not working behind QAbstractProxyModel

I need to swap the rows and columns of a QSqlRelationalTableModel. After a lot of searching, I wrote a little proxymodel to flip the rows and the columns. It is partly working. The relations in the table are resolved and showed but the dropboxes to…
logsoft
  • 61
  • 8
1
vote
1 answer

Filter contents of a QTableView connected to SQLite database via QSqlTableModel

How can I filter the contents of a QTableView that is connected to a SQLite database via QSqlTableModel? For example, if the database contains a "name" column, I want to show only the rows where the name is "Jack"?
MR.JD
  • 45
  • 1
  • 8
1
vote
2 answers

QSqlDatabase Connecting to Multiple Databases

I am having issues attempting to connect to two different databases in one Qt Application. I have my information database that stores all the information collected by the application and the new Log database which allows me to track all the changes…
JordanTSI
  • 13
  • 2
  • 6
1
vote
1 answer

QSQLDatabase (using SQLite) takes long time to open a database

I have developed an application win QT which uses SQLIte database. The copy of database is located on each site. On one site let's say site 'BOB1' it works perfectly without any problem. But when we try to use it on another site lets say 'BOB2'…
user1703942
  • 317
  • 3
  • 15
1
vote
1 answer

QSqlField real type name

i'm looking for a way to retrieve real database type name from qtsql model. Unfortunately QVariant::typeToName(field.type()) Where field is QSqlField type gives me already mapped type to some Qt type. Is it possible to get real names using Qt?
floreks
  • 33
  • 3
1
vote
1 answer

QSqlDatabase: QTDS driver not loaded

I have a program which uses QTDS driver. in development environment everything workds fine. in production i have both the TDS plugin and sybdb available with appropriate rights. -rwxr-xr-x 1 foxprd foxprd 47880 Jan 29 17:33…
user2346536
  • 1,464
  • 2
  • 21
  • 43
1
vote
1 answer

Trying to connect to Postgres with Qt

Currently I am working on a learning by doing project that should use a database to save the data. I am using Qt C++ and I have installed postgres.app. My first attempt to write a connection method is mainly taken from this source…
Jan Hettenkofer
  • 139
  • 4
  • 13