I cant connect my Qt to Sqlite
-
Hi there! I'm new in Qt and I want to learn more about it..
When I try to connect my database in Sqlite to Qt I Encounter these errors. Please help me to fix my codes..
thanks in advance..^_^here is my code:
@#include "mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QtGui/QApplication>int main(int argc, char *argv[])
{
QApplication app(argc, argv);//MainWindow mainWindow; //mainWindow.setOrientation(MainWindow::ScreenOrientationAuto); //mainWindow.showExpanded(); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("C:\sqlite3\sample.db"); if(!db.open()) { qDebug(); << db.lastError(); qFatal("Failed to connect!"); } else qDebug("Connected."); return app.exec();
}
@I got these error:
error: expected primary-expression before '<<' token
error: invalid use of incomplete type 'struct QSqlError'
error: forward declaration of 'struct QSqlError' -
You have written on line 19: @qDebug(); << ...@ There shouldn't be ; there.
-
[quote author="secretNinja" date="1304147863"]You have written on line 19: @qDebug(); << ...@ There shouldn't be ; there.[/quote]
Yes, that ; must be causing the error.
Btw there are really nice articles about SQLite in "Forum Nokia.":http://wiki.forum.nokia.com/index.php/CS001504_-_Creating_an_SQLite_database_in_Qt
Regards,
Leon -
so what should be there? :) thanks..
-
ok thanks..I'll try the link..:)
-
Only remove ; .
-
[quote author="wenks21" date="1304148870"]so what should be there? :) thanks..[/quote]
Just follow secretNinja's advice and remove ; because the << operator is overloaded for "qDebug":http://doc.qt.nokia.com/latest/qdebug.html
You may also need to call text() because qDebug does not expect QSqlError:
@
qDebug() << db.lastError().text();
@ -
when i remove that line of codes. I got these errors:
error: QSqlDatabase: No such file or directory,
error: 'QSqlError' does not name a type,
error: 'QSqlDatabase' does not name a type,I'm very sorry for being newbie.. :)
-
[quote author="wenks21" date="1304150060"]when i remove that line of codes. I got these errors:
error: QSqlDatabase: No such file or directory,
error: 'QSqlError' does not name a type,
error: 'QSqlDatabase' does not name a type,I'm very sorry for being newbie.. :)
[/quote]
Make sure you have included all required headers and include the "QtSql Module ":http://doc.qt.nokia.com/latest/qtsql.html at your pro file:
@QT += sql@
If you are a newbie it is better to stick to some examples and tutorials :)
Regards,
Leon -
wow thank you very much! Now it's working.. :)
-
I still have a question. Why is that when I re-run the program the console says that 'Cannot retrieve debugging output!'
-
[quote author="wenks21" date="1304151460"]I still have a question. Why is that when I re-run the program the console says that 'Cannot retrieve debugging output!' [/quote]
Please check this thread from Qt Center: "Cannot retrieve debugging output!":http://www.qtcentre.org/threads/37438-Cannot-retrieve-debugging-output!
Please do not use a single thread for multiple question from different issues because after that it will be difficult to track down the main issue.
Best regards,
Leon -
ok i'm sorry.. thanks again it helps a lot..:)
-
This helps me a lot. :)
[quote author="leon.anavi" date="1304149339"][quote author="wenks21" date="1304148870"]so what should be there? :) thanks..[/quote]Just follow secretNinja's advice and remove ; because the << operator is overloaded for "qDebug":http://doc.qt.nokia.com/latest/qdebug.html
You may also need to call text() because qDebug does not expect QSqlError:
@
qDebug() << db.lastError().text();
@[/quote]
-
it's solved wenks21 ?