Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:46:46+00:00 2026-06-13T02:46:46+00:00

I am a beginner with Qt, so my question might be a bit basic.

  • 0

I am a beginner with Qt, so my question might be a bit basic.

My intention is to work with an ODBC database located in my hard drive. I have tried to open it with this code:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("");
db.setDatabaseName("c:\\database.mdb");
bool ok = db.open();
QSqlQuery query;
query.exec("SELECT name FROM results WHERE tag>10");
while (query.next()) {
    QString name1 = query.value(0).toString();
    qDebug() << name1;
}

Now, the problem is that the program can’t find the database, failing at the db.open() line. I suspect that Qt can’t open a database directly, but instead has to deal with an SQL server. Is this so? If that’s the case, I’d be grateful if you could give me some clues on how to go ahead, particularly regarding host name (is it localhost?).

Also, I am not sure of whether the path to the file must be included in DatabaseName.

PS: I have no problem shifting to a different kind of database/server, e.g. MySQL. So if your solution requires this, I’d be happy with it!

Thanks in advance

D

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:46:47+00:00Added an answer on June 13, 2026 at 2:46 am

    For future reference:

    Just as Werne Strydom said, the argument of setDatabaseName is not the database file name, but the name of the ODBC datasource that points to your database. Therefore, you need to create an ODBC that points to your database.

    The usual way to do this (in Windows) would be to go to Control Panel\System & security\Administrative tools\Data Sources (ODBC). But if you’re in a 64-bit machine and want to work with a 32-bit driver, go instead to C:\windows\SysWOW64 and run odbcad32.exe

    When you do this, a dialog opens (same dialog regardless of 64/32-bits). Here you create your ODBC, give it a name and link it to your actual database. In my case, as I am working with a local database, I used the “User DNS” tab.

    Then, back in Qt, you put that ODBC name as argument for setDatabaseName. And it works! (Or it did for me…)

    The new bit of code looks like:

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("localhost");
    db.setDatabaseName("MyDataSource");
    bool ok = db.open();
    QSqlQuery query;
    query.exec("SELECT name FROM results WHERE tag>10");
    while (query.next()) {
        QString name1 = query.value(0).toString();
        qDebug() << name1;
    }
    

    where “MyDataSource” is the name I gave to the ODCB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
Bit of a beginner question here: Say I have a block of xml: <root>
I'm a beginner programmer so this question might sound trivial: I have some text
I am a beginner in database field and this question might sound too stupid
Beginner question: I have a dictionary where the values are lists of (a variable
Absolute beginner question: I have a template file index.html that looks like this: ...
I'm new to Java and I have a beginner question: NumberFormat is an abstract
I am new to Scala, just started learning, so this is basic beginner question.
This might be a silly question, but I'm a beginner in C#, so please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.