跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  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

已排程 已置頂 已鎖定 已移動 General and Desktop
22 貼文 5 Posters 13.4k 瀏覽 1 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • D 離線
    D 離線
    developer
    寫於 最後由 編輯
    #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 條回覆 最後回覆
    0
    • B 離線
      B 離線
      broadpeak
      寫於 最後由 編輯
      #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 條回覆 最後回覆
      0
      • D 離線
        D 離線
        developer
        寫於 最後由 編輯
        #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 條回覆 最後回覆
        0
        • D 離線
          D 離線
          Dronissimo
          寫於 最後由 編輯
          #4

          try to watch

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

          1 條回覆 最後回覆
          0
          • D 離線
            D 離線
            developer
            寫於 最後由 編輯
            #5

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

            1 條回覆 最後回覆
            0
            • B 離線
              B 離線
              broadpeak
              寫於 最後由 編輯
              #6

              Do you have a VALID connection to the database?

              1 條回覆 最後回覆
              0
              • Z 離線
                Z 離線
                Zarko Markovic
                寫於 最後由 編輯
                #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 條回覆 最後回覆
                0
                • D 離線
                  D 離線
                  developer
                  寫於 最後由 編輯
                  #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 條回覆 最後回覆
                  0
                  • D 離線
                    D 離線
                    developer
                    寫於 最後由 編輯
                    #9

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

                    1 條回覆 最後回覆
                    0
                    • G 離線
                      G 離線
                      gayu
                      寫於 最後由 編輯
                      #10

                      Are u using QMYSQL. Are u working in qt4

                      1 條回覆 最後回覆
                      0
                      • D 離線
                        D 離線
                        developer
                        寫於 最後由 編輯
                        #11

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

                        1 條回覆 最後回覆
                        0
                        • B 離線
                          B 離線
                          broadpeak
                          寫於 最後由 編輯
                          #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 條回覆 最後回覆
                          0
                          • D 離線
                            D 離線
                            developer
                            寫於 最後由 編輯
                            #13

                            there is not db path set @broadpeak

                            1 條回覆 最後回覆
                            0
                            • G 離線
                              G 離線
                              gayu
                              寫於 最後由 編輯
                              #14

                              So u've to set the path for db

                              1 條回覆 最後回覆
                              0
                              • D 離線
                                D 離線
                                developer
                                寫於 最後由 編輯
                                #15

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

                                1 條回覆 最後回覆
                                0
                                • B 離線
                                  B 離線
                                  broadpeak
                                  寫於 最後由 編輯
                                  #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 條回覆 最後回覆
                                  0
                                  • D 離線
                                    D 離線
                                    developer
                                    寫於 最後由 編輯
                                    #17

                                    it can open the database @broadpeek

                                    1 條回覆 最後回覆
                                    0
                                    • G 離線
                                      G 離線
                                      gayu
                                      寫於 最後由 編輯
                                      #18

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

                                      1 條回覆 最後回覆
                                      0
                                      • B 離線
                                        B 離線
                                        broadpeak
                                        寫於 最後由 編輯
                                        #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 條回覆 最後回覆
                                        0
                                        • D 離線
                                          D 離線
                                          developer
                                          寫於 最後由 編輯
                                          #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 條回覆 最後回覆
                                          0

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved