Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I cant connect my Qt to Sqlite
Forum Updated to NodeBB v4.3 + New Features

I cant connect my Qt to Sqlite

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 12.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wenks21
    wrote on last edited by
    #1

    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'

    1 Reply Last reply
    0
    • S Offline
      S Offline
      secretNinja
      wrote on last edited by
      #2

      You have written on line 19: @qDebug(); << ...@ There shouldn't be ; there.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        [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

        http://anavi.org/

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wenks21
          wrote on last edited by
          #4

          so what should be there? :) thanks..

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wenks21
            wrote on last edited by
            #5

            ok thanks..I'll try the link..:)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              secretNinja
              wrote on last edited by
              #6

              Only remove ; .

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leon.anavi
                wrote on last edited by
                #7

                [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();
                @

                http://anavi.org/

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wenks21
                  wrote on last edited by
                  #8

                  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.. :)

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    leon.anavi
                    wrote on last edited by
                    #9

                    [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

                    http://anavi.org/

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      wenks21
                      wrote on last edited by
                      #10

                      wow thank you very much! Now it's working.. :)

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        wenks21
                        wrote on last edited by
                        #11

                        I still have a question. Why is that when I re-run the program the console says that 'Cannot retrieve debugging output!'

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          leon.anavi
                          wrote on last edited by
                          #12

                          [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

                          http://anavi.org/

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            wenks21
                            wrote on last edited by
                            #13

                            ok i'm sorry.. thanks again it helps a lot..:)

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              owenzhao
                              wrote on last edited by
                              #14

                              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]

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                francomartins
                                wrote on last edited by
                                #15

                                it's solved wenks21 ?

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved