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

Subclassing QSqlTableModel to setup database

I'm trying to subclass QSqlTableModel so that the constructor will set up the database that is needed for the model. My code looks something along the lines of: MyClass::myClass( QObject* parent, QSqlDatabase data ) :QSqlTableModel(parent,…
Paul
  • 370
  • 3
  • 12
0
votes
1 answer

Qt4: how to transmit mysql query between threads?

My application reads mysql database and draws table based on mysql queries. I want to use progressbar but i can't because QSqlDatabase processes freeze my main window. I read somewhere, i should separate threads of gui and mysql processes and use…
r_spb
  • 75
  • 1
  • 14
0
votes
1 answer

How to get signal of qsqldatabase?

I want signal emitted by qsqldatabse object Eg: I have Qsqldatabase db; connect (&db, SIGNAL(signal_like_exec()), this, SLOT(any_slot())); I'm not getting signal in connect() for db Is there any way to do this?
Vinay Kulkarni
  • 81
  • 1
  • 10
0
votes
0 answers

Is it possible to modify record return by QSqlQuery select query?

Is it possible to modify record returned by QSqlQuery select query? I have something like this: QSqlQuery query(database); query.prepare('Select * from `table`); query.exec(); query.seek(15); query.record().setValue("columName", "newValue"); I am…
Artem E
  • 369
  • 1
  • 4
  • 19
0
votes
1 answer

How to use QSqlTableModel along with QTreeView

I am using sqlite database (webscrap.db). I want to make the model to display the data in the field "name" of the database table "userin". How can i fetch data using QSqlTableModel and display it with QTreeView ?
amg
  • 45
  • 1
  • 8
0
votes
0 answers

Reading excel file (.csv) using ODBC driver in Qt

I have a csv file that I'd like to parse in Qt. I'd like to use the sql plugin, but I'm not sure how to get things set up. I currently am unable to open the .csvfile from my Qt app--I have to manually open it then start my app in hopes to query from…
Rachael
  • 1,965
  • 4
  • 29
  • 55
0
votes
1 answer

Connecting to ACCDB using QT5

What I'm trying to do is find all .db (Paradox) files from a directory and save them to .mdb. The thing is, to try if this works properly I've tried first opening an ACCDB (.db and .mdb I'll be using are resources only available in my school's…
0
votes
1 answer

What's difference in connect a QSqlDatabase?

Many tutorials of QSqlDatabase start a database like this: QSqlDatabase cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id)); Would it be different from this: QSqlDatabase cn; cn.addDatabase("QSQLITE",QString::number(id)); or…
Nyaruko
  • 4,329
  • 9
  • 54
  • 105
0
votes
1 answer

Database connecting closed Gui

I have the problem thats my programm Gui was closed. For example I put this code in a pushButtonClicked method: database->addDatabase("QMYSQL", "conn1"); database->setHostName("127.0.0.1"); database->setPort(3306); database->setDatabaseName( "mydb"…
thelittlePanda
  • 101
  • 2
  • 10
0
votes
1 answer

Qt Threading code different behavior in MAC,Linux and Windows

I have written code for a server which accepts connections from different clients. Each client is serviced in different threads. Each thread accesses a database to get data and then updates this data to all the clients connected to server. 1) For…
AmarSneh
  • 103
  • 1
  • 6
0
votes
1 answer

Qt -- program that uses SQLite crashes

I have an application that uses SQLite via Qt libraries (I mean QSqlDatabase). When I run this application from Qt Creator (both in Debug and Release configurations), all works as expected, but when I trying to start an application via .exe file…
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
0
votes
0 answers

QSqlDatabase no error printed and no output?

I use QSqlDatabase to insert data to mysql. First, I define a class named Inserter: class Inserter { QSqlDatabase db_connection; public: Insert() { db_connection = QSqlDatabase::addDatabase("QMYSQL"); …
stamaimer
  • 6,227
  • 5
  • 34
  • 55
0
votes
1 answer

QtSql Fuzzy search

I am working with QSQLITE database in qt and attempt to implement fuzzy search in our program our sql query is something like this: select name from things where name like '%arg%' it's not the same the query is longer has joins and etc. I tried…
Zoli
  • 588
  • 2
  • 8
  • 13
0
votes
1 answer

Cannot load sqlite.dll with QSQLITE2 Qt plugin

I use QtCreator + mingw. I have compiled QSQLITE2 plugin. I simply entered plugin directory in Qt source code: c:\Qt\Qt5.2.0\5.2.0\Src\qtbase\src\plugins\sqldrivers\sqlite and I built it with my sqlite 2.8.17 that I have locally (as dll and…
Googie
  • 5,742
  • 2
  • 19
  • 31
-1
votes
1 answer

How to add new rows in a database?

I am creating a simple app for modifying SQLite databases. I am trying to add new rows in MainWindow::on_AddButton_clicked() method, but when I use the MainWindow::on_reselectTable_clicked() method where I reselect SqlTableView, all new rows are…
1 2 3
10
11