1

Independently of the parameter that I set to the setDatabaseName function, the open function always returns true. Is that normal?

For example:

If I ran the following code:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("");

if( db.open() )
{
    qDebug() << "open";
}

The open function returns true.

What I expect is that the open function only returns true if it was successfully connected to the database.

I already find some topics but none solved my problem:

Qt 5.5 QSqlDatabase::open() always returns true?

QSqlDatabase::open() always returns true

QSqlDatabase open always returns true after update

I am using SQLite and Qt 5.3.2.

4
  • 3
    For sqlite it creates a database if the file does not exist. Although I am not sure what it is supposed to do if you give it a blank file name. Commented Dec 12, 2016 at 20:32
  • @drescherjm, thanks. I didn't know that. So, if I need to check if the database file exists, do I need to do it "manually"? Commented Dec 12, 2016 at 20:35
  • In my code I check if the file exists. Commented Dec 12, 2016 at 20:36
  • Thanks, @drescherjm. I did it using: QFile::exists( dbPath ). Commented Dec 12, 2016 at 20:42

1 Answer 1

2

It is normal, because the connection succeeds for you.

Specifically, if a database doesn't exist, sqlite will create it. The connection will fail only if the database cannot be created with a given filepath.

If you want to check if a file exists, use QFile::exists().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.