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

Error on Submit after Inserting a Row in QSqlTableModel - No Fields to update

I have a SQLite database table with the following schema: TABLE IenState ( Id primary key, NetId integer, NodeId integer, DevType text Qos integer ) Using a DB Browser utility I inserted a record in the table. I…
Jatala
  • 93
  • 1
  • 11
2
votes
1 answer

Connection still open after QSqlDatabase goes out of scope

Why does the following code print true instead of false? int main(int argc, char *argv[]) { QApplication a(argc, argv); if (!openDatabase()) return 1; // false means don't open closed connections. QSqlDatabase db2 =…
2
votes
2 answers

sql database design for conference room scheduler

I am working on designing a database for conference room scheduler application. And we have rooms that can be merged to create a bigger one. for example, Room A can be merged to Room B to hold 64 people. I have attached my Database design below I…
zazzu
  • 37
  • 9
2
votes
0 answers

Can not load QOCI driver

I had compiled oracle driver and opened successfully with Qt a few days ago, but today, I got into trouble using the same code! Here is the code: bool isAvailable = QSqlDatabase::isDriverAvailable("QOCI"); if(isAvailable) qDebug() << "QOCI…
Mr.Guo
  • 21
  • 4
2
votes
1 answer

pyinstaller QSqlDatabase: QSQLITE driver not loaded QSqlDatabase: available drivers:

I have witten a GUI app using pyqt5 and includes a QtSql database QSQLITE. The app works perfectly. However when I run pyinstaller to a package my app, the app runs until the time where it has to invoke QtSQL DATABASE QSQLITE. This is the error I…
2
votes
1 answer

How to link an editable QComboBox to a database

I'm using Pyside to create a combobox that draws from a sqlite database. Users can select one of the existing items or add a new item. Users see the item names (called "param"), but I need access to the item ids from the database. So, there are two…
davideps
  • 541
  • 3
  • 13
2
votes
1 answer

QSqlQuery Insert into database doesn't work

I use PyQt4.QtSql.QSqlQuery. I get values from a Form and want to Insert them into the database. I open a connection and try to Add, but there are no values in the database and no errors. What's the problem? db =…
2
votes
2 answers

cx_freeze python sqlite3 database doesn't work after build.exe?

Basically i have a pyqt application that uses a sqlite3 database, now I used Cx_Freeze to turn it into an executable. I find that the database and queries run perfectly when they are ran as a .py, but after the cx_freeze conversion to .exe the gui…
hpca01
  • 370
  • 4
  • 15
2
votes
1 answer

Search SQL Database with multiple string

I have table like column1 column2 column3 red circle 0 red line 1 green circle 1 green circle 0 What I need is search the database with multiple input string, where…
Haris
  • 13,645
  • 12
  • 90
  • 121
2
votes
1 answer

Database uses windows authentication instead of server

I have a local MSSQL database with several users. I use server authentication in Management Studio successfully, but when I try to connect the database in QT it uses local windows username instead of the one I…
Charlie
  • 197
  • 1
  • 13
2
votes
1 answer

How do I set up a embedded MySQL server in Qt

I tried this code from the MySQL reference but it crashes when it comes to the point mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");. Does somebody has a working exmaple for a embedded server? #include static char…
Blacky
  • 111
  • 1
  • 1
  • 5
2
votes
2 answers

Why is my sqlite query so slow in Qt5?

In Qt5.4 using QSqlDatabase with sqlite3 on Ubuntu14.04 64bit: First I open and call transaction() on the db. Next I make 54 individual insert queries, each prepared, each deleted after execution. Finally I call commit(). All calls complete without…
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
2
votes
1 answer

Retrieving output of MySQL stored procedures with QSqlQuery

I have a MySQL server up and running. It contains database with procedures. This is one of them: CREATE PROCEDURE `handshake` () BEGIN DECLARE `is_new` INT; SELECT `value` INTO `is_new` FROM `Defaults` WHERE `key` = 'new_db'; SELECT…
smsware
  • 429
  • 1
  • 13
  • 41
2
votes
1 answer

Copy tables between sqlite databases, qt, causes error

I want to write the contents of my SQlite database on user click to another SQlite database. For this I am trying to make connections to two databases and do select query from one db and in transaction do insert query to another. But I gets error on…
A.J
  • 725
  • 10
  • 33
2
votes
1 answer

Managing database and connection lifetime

In Qt there are a few steps that need to be finish until a database access can be done. The very first step is to add a database by connection name: QSqlDatabase::addDatabase("QMYSQL", connectionName); After this I can use open() and close() to…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
1
2
3
10 11