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

Correct way to reconnect to database after disconnect using Qt and QSqlDatabase

What is the correct way to reconnect to database using Qt4 upon disconnect? I'm using Sql Server 2012 over ODBC. If I detect disconnection using SELECT 1 query, and then do db.close(); db.open() I receive exceptions on other opened SqlQueries (in…
dE fENDER
  • 130
  • 1
  • 8
2
votes
2 answers

Qt5/c++ generating 'Corrupted shared library list' error during debug

I have a Qt5/C++ app which appears to be working fine, but when running in debug mode (GDB), this error pops up on the QT Creator console (stderr) - OCCASIONALLY!: Corrupted shared library list: 0x7fffe8008e90 != 0x751e50 I can't find any reference…
TSG
  • 4,242
  • 9
  • 61
  • 121
2
votes
1 answer

How to lock a table in a database via Qt?

I have connected to a database in Qt using c++. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/link/to/my.db"); Simultaneously I have a Python program that connect to same database and constantly updates a table…
Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76
2
votes
1 answer

Get primaryKey programmatically with Qt

I try to get primaryKey programmatically and convert it to col name. this function not work as mast be. QString getPrimaryFiled( const QString &tableName ) { QSqlDatabase m_SqlDataBase =…
Amr Eisa
  • 33
  • 5
2
votes
2 answers

Qt5.2.0; Debian Wheezy: QSqlDatabase destructor causes segfault

I have an app that utilizes a wrapper class to access a MySQL database. Since the database is accessed at various places in the app, and the app is multithreaded, the wrapper class is used to minimize the amount of repetitive code needed to access…
1
vote
0 answers

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

I wanna make a MySQL database mapping to QTableView but i got this error: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7 My code is: from mysql.connector import…
KsOnd
  • 11
  • 1
1
vote
0 answers

How to start an immediate transaction of Sqlite in Qt?

Got troubles with using QSqlDatabase::transaction(). I want to start a transaction of Sqlite in Qt and I know this function : bool QSqlDatabase::transaction(): if(db.transaction()) { QSqlQuery query(db); // do stuff if(!db.commit()) …
1
vote
1 answer

How to read the headers of a .txt files and put them as headers of a QTableView

I have a small problem trying to propoerly parse a .txt files and show its content on a QTableView. Specifically how to extract the headers of the file and show them into a QTableView. The .txt file is composed of a first row which carries the…
EsoMars
  • 337
  • 3
  • 14
1
vote
0 answers

How to send postgresql notifications via PyQt5 QSqlDatabase?

I am writing a pyqt5 qui that interfaces with my psql DB via SQslDatabase. External-to-me processes have a listen/notify setup to communicate status to other components via the database. So, my gui needs to listen, as well as send notifications. I…
RuslanS
  • 13
  • 3
1
vote
0 answers

Qt QSql: cannot create a new MDB-File per ODBC

I am using the following code to create an NEW MS Access file using the SQL API from Qt. QString strDatabaseName; if (strExt == ".DB") { oQtDatabase = QSqlDatabase::addDatabase(_TN("QSQLITE"), _TN("Redline_DB")); strDatabaseName =…
Graphman
  • 11
  • 2
1
vote
1 answer

Python Insert Data from Qtable Widget into Ms Access with QSqlDatabase

This is what I have so far: def save_invoice(self): con = QSqlDatabase.addDatabase("QODBC") con.setDatabaseName("C:/Users/Egon/Documents/Invoice/Invoice.accdb") # Open the connection con.open() # Creating a query for…
accpert.com
  • 109
  • 11
1
vote
1 answer

What could be the reason why QSQLITE ATTACH DATABASE is hanging

I'm struggling on a very uncommon failure. I had a program running which attaches a configuration database to a production database. I used the ATTACH DATABASE command from sqlite. It was working fine and I was glad with that solution. From one run…
Papageno
  • 305
  • 3
  • 15
1
vote
1 answer

How to concatenate three feilds records from one sqlite table and insert that in to one feild of the second table?

Below is my example code: from PyQt5 import QtCore, QtGui, QtWidgets, QtSql from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtSql import * import sys class Ui_MainWindow(object): def setupUi(self,…
user3030327
  • 411
  • 1
  • 7
  • 19
1
vote
1 answer

Conditional Group By on Selected rows in KDB/Q-Sql

I have a requirement where I have to execute multiple queries and perform group by on a column with where clause , group by column is fixed and where condition will be perform on fixed column with variable criteria . Only Column name and …
sam
  • 47
  • 6
1
vote
2 answers

KDB/Q-sql Dynamic Grouping and con-canting columns in output

I have a table where I have to perform group by on dynamic columns and perform aggregation, result will be column values concatenating group-by tables and aggregations on col supplied by users. For example : g1 g2 g3 g4 col1 col2 A D F …
sam
  • 47
  • 6
1 2
3
10 11