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

Connecting to Firebird database in Qt

I have local database, that I want to connect with. Here is my code, but unfortunately it doesn't work. QString servername = "SYSDBA@localhost"; QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setHostName(servername); …
karollo
  • 573
  • 8
  • 22
0
votes
0 answers

connect to MySQL data base with QSqlDatabase pyqt4

im new to python code, QT and MySQL, i wanna show a data base in a QtableView but i cant it. i dont know whats happend, the error is ('QSqlDatabase' object has no attribute 'setHotsName') the code is: # -*- coding: utf-8 -*- import sys import…
0
votes
1 answer

Trying to delete from database on QT table

I have a database named student with 4 fields (name , lastname, semester and studentid). I have an application connected that lets the user inserts students in the database having an option to show them on a table after the inserts. Table has a…
pfoutzop
  • 65
  • 11
0
votes
0 answers

Database Error "Parameter count mismatch" on C++

So I am missing something on the code but I cant figure out what this is. I am trying to isert some values from text fields into the database but I get the error of the article name. Can somebody help? I am posting the isert code and the buttons…
pfoutzop
  • 65
  • 11
0
votes
1 answer

query_exec(SELECT * FROM myTable) fails on pyqt5

i have a function like below def updateExpenseEntryToDb (self): self.day = self.line_edit1.text() self.category = self.line_edit2.text() self.amount = self.line_edit3.text() db = QSqlDatabase.addDatabase('QSQLITE') …
0
votes
1 answer

using query.exec_ from PYQT5 to insert multiple values into QSqlDatabase

my table has 3 columns (1)id (2)firstname (3)lastname My local variables have the following values id = '1' firstname = 'xxx' lastname = 'yyy' below query works and insert only value of id in the DB query.exec_("insert into sportsmen (id) …
0
votes
1 answer

QSqlDatabase::record(const QString &tablename) returns empty record

I have already read this question. I want to get field names of a table using method QSqlDatabase::record(const QString &tablename). But it always returns an empty record. I can query database tables by QSQLQuery properly. My database is a SQL…
m.taheri
  • 309
  • 3
  • 21
0
votes
2 answers

"Unable to find table" error when calling QSqlTableModel setTable method

I need to populate a QTableView by retrieving data from a QSqlTableModel object. I use following commands to make a connection to a sql server database: QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); QString connectionTemplate = "DRIVER={SQL…
m.taheri
  • 309
  • 3
  • 21
0
votes
0 answers

Qt - QSqlDatabse open() blocked?

I have a weird behaviour on my application using QSqlDatabase. This is the simple code i'm using: QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL",…
BEPP1
  • 945
  • 2
  • 11
  • 15
0
votes
1 answer

QSqlDatabase not opening

I am unable to understand what I am doing wrong here. I am using Qt 5.7.1 and the code is as follows: QString filePath = QCoreApplication::applicationDirPath(); QString dbPath = QDir(filePath).absoluteFilePath("../../../Database"); m_db…
user2522981
  • 163
  • 3
  • 18
0
votes
0 answers

Error: SIGABRT Signal meaning : Aborted" when trying to connect to the database by QT

Error: The inferior stopped because it received a signal from the Operating System. Signal name : SIGABRT Signal meaning : Aborted Code Snippet: #include "postgresql.h" #include QString…
0
votes
1 answer

Remote connectivity issue with QSqlDatabase [ODBC]

I'm trying to make a connection to a remote SQL server : QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3"); db.setDatabaseName(QString("Driver={SQL Server Native Client 10.0};Server=X.X.X.X;Database=/*DB*/;")); db.setUserName("sa"); …
IMAN4K
  • 1,265
  • 3
  • 24
  • 42
0
votes
1 answer

QSqlDatabase: unable to use connection from QThread

I have two QSqlDatabase database connections, "local" and "remote". I have a function that uses both connections (get data from local db and send to remote db). When I run this function from my QThread class, the local connection works (I got…
Peder
  • 157
  • 1
  • 1
  • 9
0
votes
0 answers

QSqlQuery very slow in Qt5 sometimes

I develop application based on Qt5. In my application I use sqlite3 database. And then I found one problem that the sqlite3 QSqlQuery.next() is very slow when my app first launch.
Nick Sun
  • 141
  • 3
  • 10
0
votes
1 answer

QSqlDatabse : the program has unexpectedly finished?

Everytime I declare QSqlDatabse connection in the header file of my DatabaseManager class, my application crashes with a message says: the program has unexpectedly finished ?! If I put the declaration inside the source file, my application works…
McLan
  • 2,552
  • 9
  • 51
  • 85