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 can't Insert a new data to SQLITE
Forum Updated to NodeBB v4.3 + New Features

I can't Insert a new data to SQLITE

Scheduled Pinned Locked Moved General and Desktop
22 Posts 9 Posters 23.3k 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #8

    How far did you get? What is the output your program generated?

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

      It only connects to the database.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #10

        [quote author="wenks21" date="1305615786"]It only connects to the database.[/quote]
        So... after the "Connected." output, you get nothing else? Your code suggests that you should either get an error or an OK for both the queries you try to execute. If you get an error, please show it.

        Edit:
        I think you are messing up the constructors of your QSqlQuery object. Try to use the constructor that takes a QSqlDatabase argument.

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

          yes exactly. after the 'Connected' output nothing else happened.

          how to do the constructor that you are saying? any samples?

          thanks a lot!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #12

            You might try the QSqlQuery documentation to learn about the different constructors for that class...

            If you get no other output, you might try to set a breakpoint at the place where you last get output, and follow the program execution statement by statement by running in debug mode (F5 in Qt Creator).

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

              ok thanks for the info!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aureliocano
                wrote on last edited by
                #14

                Hi wenks21,

                Did you resolve your problem?
                I am in the same point... :(

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  solareclectic
                  wrote on last edited by
                  #15

                  You need to open the actual file before opening the database within, like so....
                  @
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  QFile newdbfile(QDir::toNativeSeparators(QDir::homePath()+"/test.sqlt"));
                  db.setDatabaseName(newdbfile.fileName());

                  if (newdbfile.open(QIODevice::ReadWrite))
                  {
                     if(!db.open())
                     {
                         qFatal("Failed to connect!");
                     }
                     else
                     qDebug("Connected.");
                  }
                  

                  @

                  notice, that I wrapped your db.open() with a newdbfile.open().
                  also, this little dance I did sure makes it easy to move around 'tween machines and platforms
                  @

                  QFile newdbfile(QDir::toNativeSeparators(QDir::homePath()+"/test.sqlt"));
                  db.setDatabaseName(newdbfile.fileName());
                  

                  @

                  I built and ran this code inserted into your code. It works.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #16

                    [quote author="solareclectic" date="1309313429"]You need to open the actual file before opening the database within, like so....[/quote]

                    You do not need to open the file. If the code above does not work it is most likely that you

                    • have no permission to write to the C:/sqlite3 directory
                    • have no directory C:/sqlite3, which prevents the QSQLITE driver from creating the file (this might be the reason why opening - and thus creating - the file solves your problem)
                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      silver47
                      wrote on last edited by
                      #17

                      Hi.
                      I tested your code and it works fine. Did you forget to add QT += sql in your .pro file?

                      sorry for my english :(

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aureliocano
                        wrote on last edited by
                        #18

                        Hi,

                        I have the same error with solareclectic code, The db is created and after 'Connected' output I don't get nothing, the table is not created.

                        Yes, I added QT += sql in my .pro file.

                        There is something I omitted because I didn't give importance and it is that I use QSQLITE2 driver instead of QSQLITE, could this be the error?

                        Thanks!

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          silver47
                          wrote on last edited by
                          #19

                          Qt doc:

                          bq. The Qt SQLite 2 plugin is offered for compatibility. Whenever possible, use the version 3 plugin instead.

                          sorry for my english :(

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dialingo
                            wrote on last edited by
                            #20

                            If more than one instance of the application is running the database can be locked. Do not step into this trap as I did several times.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              aureliocano
                              wrote on last edited by
                              #21

                              Solved.

                              Using QSQLITE works perfectly. With QSQLITE2 doesn't.

                              Thanks a lot!

                              1 Reply Last reply
                              0
                              • W wenks21

                                I tried to insert new data to the database. here's my code:
                                @#include "mainwindow.h"
                                #include <QtSql/QSqlDatabase>
                                #include <QtDebug>
                                #include <QtGui/QApplication>
                                #include <QtSql>

                                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\sample1.db");
                                
                                if(!db.open())
                                {
                                    qFatal("Failed to connect!");
                                }
                                
                                else
                                
                                qDebug("Connected.");
                                
                                QSqlQuery qry;
                                
                                qry.prepare("insert into sample1.db (firstname, lastname) values ('John','Dean')");
                                if (!qry.exec&#40;&#41;)
                                
                                    qDebug() << qry.lastError();
                                
                                else
                                  qDebug("Inserted");
                                
                                
                                db.close();
                                return 0;
                                
                                return app.exec();
                                

                                }
                                @

                                It has no errors but when I look into my sqlite using SELECT statement to see if it works, nothings happened the data that I want to add is not there.

                                thanks in advance!

                                A Offline
                                A Offline
                                Abrar Shariar
                                wrote on last edited by
                                #22

                                @wenks21 said:

                                qry.prepare("insert into sample1.db (firstname, lastname) values ('John','Dean')");

                                check this above line in your code:
                                The sql query should be : insert into table_name(column_name,column_name)values (value,value);

                                Your are missing out the table_name instead adding your db name. That's wrong.

                                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