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. [solved]qt sql insert statement system not working
Forum Updated to NodeBB v4.3 + New Features

[solved]qt sql insert statement system not working

Scheduled Pinned Locked Moved General and Desktop
22 Posts 5 Posters 13.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.
  • D Offline
    D Offline
    developer
    wrote on last edited by
    #1

    @ qDebug() << query->exec(QString("insert into accounts (username,password,website,agroup)values('%1','%2','%3','%4')").arg(username,password,website,group));@
    the following code used.
    tried a lot of things and searched a lot debug retruns true but nothing happens nothing is inserted

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on last edited by
      #2

      For INSERT, this solution is better (int id, QString tag):
      @
      QSqlQuery qry;
      qry.prepare( "INSERT INTO tags (id, tag) VALUES (:id, :tag)" );
      qry.bindValue( ":id", id );
      qry.bindValue( ":tag", tag );
      if( !qry.exec() )
      qFatal( "Failed to add tag" );
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        developer
        wrote on last edited by
        #3

        idid that does not works
        @ query->prepare("INSERT INTO accounts (username,password,website,agroup) VALUES (:username,:password,:website,:agroup)");
        query->bindValue(":username" , username);
        query->bindValue(":password" , password);
        query->bindValue(":website" , website);
        query->bindValue(":agroup" , group);
        qDebug() << query->exec();//always returns true@

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dronissimo
          wrote on last edited by
          #4

          try to watch

          @qDebug () << query->lastQuery() ;@

          1 Reply Last reply
          0
          • D Offline
            D Offline
            developer
            wrote on last edited by
            #5

            "INSERT INTO accounts (username,password,website,agroup) VALUES (:username,:password,:website,:agroup)"

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              Do you have a VALID connection to the database?

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zarko Markovic
                wrote on last edited by
                #7

                Modify and use the test code below to locate the cause of error. Also make sure the sql module is included in your project file (QT += sql).

                @ QString tDbFile = "/path../apptempdir/db.sql";
                // Try to connect
                tQSqlDatabase = QSqlDatabase::addDatabase("QSQLITE");
                tQSqlDatabase.setDatabaseName( tDbFile );
                if (!tQSqlDatabase.open())
                {
                QMessageBox::information (this, "Error", "Db connection error");
                return false;
                }

                QString tQueryString = "INSERT...";
                QSqlQuery tSqlQuery;
                tSqlQuery.exec(tQueryString);

                QString tQueryResult = tSqlQuery.lastError().databaseText() + "\n" + tSqlQuery.lastError().driverText();
                QMessageBox::information (this, "Result", tQueryResult);
                @

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  developer
                  wrote on last edited by
                  #8

                  the result:
                  1:the QT += sql is included in the project file
                  2:the resut of tqueryresult
                  "No query
                  Unable to fetch row"

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    developer
                    wrote on last edited by
                    #9

                    @broadpeak yes the database.open() returns true and also i am able to create table QSqlTableModel is working fine

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gayu
                      wrote on last edited by
                      #10

                      Are u using QMYSQL. Are u working in qt4

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        developer
                        wrote on last edited by
                        #11

                        @gaya i am using QSQlITE yes i am working in qt4

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          broadpeak
                          wrote on last edited by
                          #12

                          Try this codesnippet:

                          @

                          void YourClass::init()
                          {
                          //...
                          initDB("../../../db/mydb.db3"); // of course your db path goes here
                          //...
                          }

                          void YourClass::initDB(QString dbPath)
                          {
                          vc2DB = QSqlDatabase::addDatabase("QSQLITE");
                          vc2DB.setDatabaseName(dbPath);
                          vc2DB.open();
                          QSqlQuery query;
                          query.exec("PRAGMA foreign_keys = ON;");
                          bool b = query.exec("select id from role where code = 'SU'");
                          if (!b) {
                          std::cerr << "Failed to open database: " << dbPath.toAscii().data() << std::endl;
                          exit(-1);
                          }
                          }
                          @

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            developer
                            wrote on last edited by
                            #13

                            there is not db path set @broadpeak

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              gayu
                              wrote on last edited by
                              #14

                              So u've to set the path for db

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                developer
                                wrote on last edited by
                                #15

                                why the table has been created fine.
                                also where to seta good path for ana pplication

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  broadpeak
                                  wrote on last edited by
                                  #16

                                  [quote author="developer" date="1353499828"]there is not db path set @broadpeak[/quote]

                                  What? I don't understand this.

                                  But! If the open() function can't open your database, it will open with the given name an another database in QSQLITE! See your databaseS.

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    developer
                                    wrote on last edited by
                                    #17

                                    it can open the database @broadpeek

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      gayu
                                      wrote on last edited by
                                      #18

                                      even though the file does not exist, the SQLite will try to create it.

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        broadpeak
                                        wrote on last edited by
                                        #19

                                        [quote author="developer" date="1353500452"]it can open the database @broadpeek[/quote]

                                        Anyway, can you run (from your code) a simple SELECT on your database? Have you got a resultset?
                                        Have you grant at all for your tables?

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

                                          ohhhhhhhhhh acutally its working with setting url a different databse opens and with just typing passwordmanager as databse name it works actaully the problem is with the table view thanks all problem solved i tried select staement and i got the result

                                          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