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

Qt QSqlDatabase QSQLITE driver not loaded error

I am getting the driver not loaded error when trying to open an sqlite database with Qt 5.15.0 on Windows. Here is my output: QSqlDatabase: QSQLITE driver not loaded QSqlDatabase: available drivers: QSQLITE QPSQL QPSQL7 QODBC QODBC3 "Driver not…
1
vote
2 answers

Connect to multiple databases in PyQt5

I need to connect to two different databases and servers. Right now I can only connect to one database using this code: def createConnection() global db db = QSqlDatabase.addDatabase('QODBC') db.setDatabaseName('DRIVER={SQL…
kokoro
  • 21
  • 3
1
vote
1 answer

QSqlquery prepare() and bindvalue() not working?

I have this code for managing logins, here is a small bit of it: void Login::on_pushButton_clicked(){ QString user, password; user = ui->username->text(); password = ui->pass->text(); QSqlQuery qry; qry.prepare("SELECT id, name…
Niko
  • 41
  • 1
  • 6
1
vote
1 answer

PyQt5 QtSql access database in QThread

OK, now that I figured out the the whole persistent QSqlDatabase in a MDI application, I attempted to access it in a QThread: class LoadWorker(QThread): totalSignal = pyqtSignal(int) countSignal = pyqtSignal(int) def __init__(self,…
Elcid_91
  • 1,571
  • 4
  • 24
  • 50
1
vote
1 answer

PostgreSql, QSqlDatabase: Failed to open Database

My system: Windows 10 Pro 16299, Qt, PyQt 5.11.2, Python 3.6, PostgreSql 10 I tried to use QTableView/QSqlTableModel for in my gui to work with postgresql data. However, I am not able to open the database. I get the error message “Driver not loaded…
Joachim W.
  • 25
  • 4
1
vote
1 answer

Qt - Applications sharing the same database

I have an application using: QString databasePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); databasePath += "/MyApp.db"; qApp->setProperty("MYAPP_DATABASE_PATH", databasePath); to create a path for the database Is…
Gabriel_Br
  • 119
  • 1
  • 9
1
vote
0 answers

use in-memory sqlite in QT

I created a QSqlQuery to get some value from an in-memory database, which been set by setDatabaseName(":memory:") When I execute the query, it gives me a QSqlError QSqlError("", "Unable to fetch row", "No query") However, when I change the…
Jason Lei
  • 47
  • 4
1
vote
1 answer

PyQt QSqlQuery.prepare() returns false

In PyQt4 I create a QSqlDatabase like slpath = 'path/to/my/db.sqlite' db = QSqlDatabase.addDatabase('QSPATIALITE') db.setDatabaseName(slpath) This seem to work. Now I try to UPDATE a table layer_styles as follows: query = QSqlQuery(db) #db cp.…
Jochen Schwarze
  • 292
  • 6
  • 27
1
vote
1 answer

Qt multithreaded transactions in SQLite

How can one get access to transactions using SQLite through QSqlDatabase ? I open the database as follows: m_db = QSqlDatabase::addDatabase("QSQLITE", connection_name); m_db.setDatabaseName(db_name); m_db.open(); I create two such connections - for…
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
1
vote
1 answer

QSqlDatabase sqlite database works on Android an not on iOS - QT 5.9 cross platform

so my problem, I trying to create a sqlite database with QT. So...i use this code: QString dbName = "apo.sqlite"; db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(dbName); if( QFile::exists(dbName)) { db.setDatabaseName(dbName); …
Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
1
vote
1 answer

QSqlDatabase::open() always returns true Qt 5.3.2

Independently of the parameter that I set to the setDatabaseName function, the open function always returns true. Is that normal? For example: If I ran the following code: QSqlDatabase db =…
KelvinS
  • 2,870
  • 8
  • 34
  • 67
1
vote
1 answer

how to make Sqlite database read only?

I have sqlite database that i want to be readonly, when it used by other gui application, so that user can't edit data, only Qt application can edit it. Here is my code: //open the database QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); …
abdallah allam
  • 73
  • 1
  • 13
1
vote
0 answers

Insert a QSqlRecord of type serial in PostgreSQL

I'm working with the Books Qt example but instead of SQLite I'm using PostgreSQL, so my main table and related tables were created with the following code: QSqlQuery q; if (!q.exec(QLatin1String("create table orders(id serial primary key, name…
Tarod
  • 6,732
  • 5
  • 44
  • 50
1
vote
2 answers

QSqlDatabase : Driver not loaded Driver not loaded

I developed a c++ library using qt. In this one I am using QSqlDatabase to query informations from a SQLite database. Notice : my library works fine in a qt desktop application (I am developing on Linux). Now my next step is to use my libray in an…
suns9
  • 875
  • 2
  • 8
  • 17
1
vote
0 answers

connecting to an Access database with qt

I'm on windows 8 using Qt 4.7.4 (dont ask why :p) I'm trying to connect to an Access database using this lines of code db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS…
Abdou
  • 330
  • 1
  • 9