Unsolved Role of .setHostName() while Connecting to DataBase in Qt
-
Hi All ,
Below are the code snippet given in Qt documentation of Qt; Connecting to Database.
I want to know what to write in the HostName. I would also like to know if I've to give the absolute path address of my .db file for .setDatabaseName (). I have a .db file in SQLite.Any help will be appreciated!
Thanks in advance!QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("bigblue"); db.setDatabaseName("flightdb"); db.setUserName("acarlson"); db.setPassword("1uTbSbAs"); bool ok = db.open();
-
@Swati777999
This is an example for MySQL, but you are using SQLite. For that just usedb.setDatabaseName("/full/path/to/flightdb");
. You can ignoresetHostName()
(or set it to"localhost"
). -
I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("C:/Users/ss/Desktop/TestData.db"); db.setHostName("localhost"); db.setUserName("swati"); db.setPassword("qtsqlite");
-
@Swati777999 Have you tried to read the documentation? It is all described on the very first page, you know.
-
@Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:
I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?
I think you have first to understand how Qt is managing databases.
QSqlDatabase
is used to define all database connection you want to use in your application.
In you case you have defined the default database connection, because you don't have give a name to the connection.Before be able to use the connection, you have to open it and then you use
QSqlQuery
to send SQL statements to the DBYou can take a look at this http://katecpp.github.io/sqlite-with-qt/ which gives you step by step explanation about SQLite usage with Qt.
-
@KroMignon Thanks for your answer!
Just like Kate (Reference: the link given by you), I've put the following conditions for getting the status of the connection in the Application Output of QtCreator but I only get the output whenever there's an error. I don't get the output when there's no error. Why is it so? Can you please explain?
bool ok = db.open(); if (!ok) { qDebug() << "Error: connection with database failed"; } else { qDebug() << "Database: connection ok"; }
-
@Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:
I don't get the output when there's no error. Why is it so? Can you please explain?
No can't explain, this don't make sense to me.
You could change to code to:bool ok = db.open(); qDebug() << "Database: connection " << (ok ? "OK" : "FAILURE");
You should always got a message.
Are you sure you have build the project before starting it? -
@Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:
Why is it so? Can you please explain?
It is not so. You get a message either way. If you execute that code. If you don't execute the code you don't get any message.
-
Hi,
One side note: unless you try to open your database on a read-only filesystem where the file does not exist, you won't have any error. This comes from SQLite, if the database file does not exist, it's created.