0

I am very new to using QT4. I am trying to use sql in an already working application. However, when I try to compile I get this error: "‘QSqlDatabase’ was not declared in this scope."

Here is the relevent code:

#include <QtSql>
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

 db.setHostName("bigblue");
 db.setDatabaseName("flightdb");
 db.setUserName("acarlson");
 db.setPassword("1uTbSbAs");
 bool ok = db.open();

I have added QT += sql to my .pro file.

The code above it literally copied from here:http://qt.nokia.com/doc/4.5/qtsql.html

Any idea what I am doing wrong?

10
  • Or if anyone has a decent tutorial on this that would also be helpful. I just cant seem to find good docs Commented Nov 15, 2009 at 23:24
  • It also says, error: QtSql: No such file or directory Commented Nov 15, 2009 at 23:30
  • If it's not finding QtSql it looks like a problem with your Qt installation Commented Nov 15, 2009 at 23:35
  • I have these packages installed: libqt4-core libqt4-debug libqt4-gui libqt4-qt3support libqt4-sql qt4-designer qt4-dev-tools qt4-doc qt4-qtconfig What do you think I am missing? Commented Nov 15, 2009 at 23:48
  • What compiler command line does make produce? Your code compiles ok for me Commented Nov 15, 2009 at 23:54

3 Answers 3

5

Ok, the problem was with my .pro file. This is the file that gave me the error:

TEMPLATE = app
QT += sql
QT = gui core
CONFIG += qt debug warn_on console
DESTDIR = bin
OBJECTS_DIR = build
MOC_DIR = build
UI_DIR = build
FORMS = ui/mainwindow.ui ui/dialog.ui ui/dialog_con.ui ui/add_ingredient.ui
HEADERS = src/mainwindowimpl.h \
src/dialogimpl.h \
src/utils.h \
SOURCES = src/mainwindowimpl.cpp \
src/main.cpp \
src/dialogimpl.cpp \
src/utils.cpp \

The problem was that QT=gui core was overriding my QT+=sql. by moving the QT+=sql to bo right above HEADERS, it compiled just fine. Thanks for the help.

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

1 Comment

I think that usually you shouldn't assign (with = )variables on a qmake file, but is better to add or remove values from them (with += or -=), or you can have some side effects as it happened to you.
2

The compiler can't find the QtSql header and therefore doesn't know about QSqlDatabase. The header is in the QtSql subdirectory, so maybe you need to explicitly specify this in the include line:

#include <QtSql/QtSql>

Alternatively you have to make sure that the QtSql directory is in the include file search path of your compiler.

2 Comments

That just made it go from 3 errors, to 10. Do you want me to post them all?
Usually the first error is the most significant one... What does the first error message say?
-1

I think you just need a

#include <QSqlDatabase>

as this is not automatically included by QtSql.

3 Comments

Just tried it, here are the new errors. error: QtSql: No such file or directory src/mainwindowimpl.cpp:9:24: error: QSqlDatabase: No such file or directory src/mainwindowimpl.cpp: In member function ‘void MainWindowImpl::not_done()’: src/mainwindowimpl.cpp:58: error: ‘QSqlDatabase’ was not declared in this scope src/mainwindowimpl.cpp:58: error: expected `;' before ‘db’ src/mainwindowimpl.cpp:59: error: ‘db’ was not declared in this scope src/mainwindowimpl.cpp:63: warning: unused variable ‘ok’
"I'm right, you're wrong" doesn't tend to make be trust you any more than the other person. Any supporting evidence for your statement?
@Dems /usr/include/qt4/QtSql/QtSql contains the line: #include "qsqldatabase.h" Is that enough evidence?

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.