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. How to reset DB table
Forum Update on Monday, May 27th 2025

How to reset DB table

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.1k Views
  • 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.
  • V Offline
    V Offline
    Vineela
    wrote on 6 Feb 2019, 12:15 last edited by Vineela 2 Jun 2019, 12:44
    #1

    this is my code where inserting json data into db table

    QJsonDocument doc = QJsonDocument::fromJson(data);
        QJsonArray array = doc.array();
            QJsonObject object = array.at(0).toObject();
            QString code = object["VillageCode"].toString();
            qDebug()<< "VillageCode" +code;
            QString code1 = object["VillageName"].toString();
            qDebug()<< "VillageName" +code1;
            QString code2 = object["RyotCode"].toString();
            qDebug()<< "RyotCode" +code2;
            QString code3 = object["RyotName"].toString();
            qDebug()<< "RyotName" +code3;
            QString code4 = object["SectionCode"].toString();
            qDebug()<< "SectionCode" +code4;
            QString code5 = object["PhNo"].toString();
            qDebug()<< "PhNo" +code5;
    
    
    
            QSqlQuery query;
            query.prepare("INSERT INTO farmer1 (VillageCode , VillageName, RyotCode, RyotName, SectionCode, PhNo) "
                           "VALUES (:villageCode, :villageName, :ryotCode, :ryotName, :sectionCode, :phNo)");
            query.bindValue(":villageCode", code);
            query.bindValue(":villageName", code1);
            query.bindValue(":ryotCode", code2);
            query.bindValue(":ryotName", code3);
            query.bindValue(":sectionCode", code4);
            query.bindValue(":phNo", code5);
            query.exec();
    
        //QTableModel model2 = new QTableModel(this);
    
    
    
    
    
        QStringRef subString(&sectioncode, 0, 10);
       qDebug()<< "Current Text"+ subString;
    
    
    
    QSqlTableModel* model2 = new QSqlTableModel(this);
    model2->setTable(QStringLiteral("farmer1"));
    model2->setFilter(("SectionCode ='"+subString+"'"));
    
    model2->select();
    model2->setHeaderData (1,Qt::Horizontal, QObject::tr ("Ryot No."));
      model2->setHeaderData (2,Qt::Horizontal, QObject::tr ("Ryot Name"));
        model2->setHeaderData (6,Qt::Horizontal, QObject::tr ("Mobile"));
         //ui->tableView->setEnabled(false);
    
    ui->tableView_2->setModel(model2);
    ui->tableView_2->hideColumn(0);
    ui->tableView_2->hideColumn(3);
    ui->tableView_2->hideColumn(4);
    ui->tableView_2->hideColumn(5);
    
    

    so here i need to clear my DB table where the value is inserting repeatedly .
    Help me out..!!!

    M 1 Reply Last reply 6 Feb 2019, 12:51
    0
    • J jsulm
      6 Feb 2019, 13:02

      @Vineela "DELETE FROM farmer1" https://www.w3schools.com/sql/sql_delete.asp

      V Offline
      V Offline
      Vineela
      wrote on 7 Feb 2019, 04:45 last edited by
      #9

      @jsulm , @VRonin yes well this worked

      query.prepare("DELETE from farmer1");
          query.exec();
      

      thank you guys.

      1 Reply Last reply
      0
      • V Vineela
        6 Feb 2019, 12:15

        this is my code where inserting json data into db table

        QJsonDocument doc = QJsonDocument::fromJson(data);
            QJsonArray array = doc.array();
                QJsonObject object = array.at(0).toObject();
                QString code = object["VillageCode"].toString();
                qDebug()<< "VillageCode" +code;
                QString code1 = object["VillageName"].toString();
                qDebug()<< "VillageName" +code1;
                QString code2 = object["RyotCode"].toString();
                qDebug()<< "RyotCode" +code2;
                QString code3 = object["RyotName"].toString();
                qDebug()<< "RyotName" +code3;
                QString code4 = object["SectionCode"].toString();
                qDebug()<< "SectionCode" +code4;
                QString code5 = object["PhNo"].toString();
                qDebug()<< "PhNo" +code5;
        
        
        
                QSqlQuery query;
                query.prepare("INSERT INTO farmer1 (VillageCode , VillageName, RyotCode, RyotName, SectionCode, PhNo) "
                               "VALUES (:villageCode, :villageName, :ryotCode, :ryotName, :sectionCode, :phNo)");
                query.bindValue(":villageCode", code);
                query.bindValue(":villageName", code1);
                query.bindValue(":ryotCode", code2);
                query.bindValue(":ryotName", code3);
                query.bindValue(":sectionCode", code4);
                query.bindValue(":phNo", code5);
                query.exec();
        
            //QTableModel model2 = new QTableModel(this);
        
        
        
        
        
            QStringRef subString(&sectioncode, 0, 10);
           qDebug()<< "Current Text"+ subString;
        
        
        
        QSqlTableModel* model2 = new QSqlTableModel(this);
        model2->setTable(QStringLiteral("farmer1"));
        model2->setFilter(("SectionCode ='"+subString+"'"));
        
        model2->select();
        model2->setHeaderData (1,Qt::Horizontal, QObject::tr ("Ryot No."));
          model2->setHeaderData (2,Qt::Horizontal, QObject::tr ("Ryot Name"));
            model2->setHeaderData (6,Qt::Horizontal, QObject::tr ("Mobile"));
             //ui->tableView->setEnabled(false);
        
        ui->tableView_2->setModel(model2);
        ui->tableView_2->hideColumn(0);
        ui->tableView_2->hideColumn(3);
        ui->tableView_2->hideColumn(4);
        ui->tableView_2->hideColumn(5);
        
        

        so here i need to clear my DB table where the value is inserting repeatedly .
        Help me out..!!!

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 6 Feb 2019, 12:51 last edited by
        #2

        @Vineela
        "clear my DB table"
        You mean delete ALL records in it ?

        V 1 Reply Last reply 6 Feb 2019, 12:53
        0
        • M mrjj
          6 Feb 2019, 12:51

          @Vineela
          "clear my DB table"
          You mean delete ALL records in it ?

          V Offline
          V Offline
          Vineela
          wrote on 6 Feb 2019, 12:53 last edited by
          #3

          @mrjj no only the table in here which i'm inserting it to

          model2->setTable(QStringLiteral("farmer1"));
          
          M J 2 Replies Last reply 6 Feb 2019, 12:56
          0
          • V Vineela
            6 Feb 2019, 12:53

            @mrjj no only the table in here which i'm inserting it to

            model2->setTable(QStringLiteral("farmer1"));
            
            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 6 Feb 2019, 12:56 last edited by mrjj 2 Jun 2019, 12:58
            #4

            @Vineela
            Hi
            most such things you do via SQL
            https://www.w3schools.com/sql/sql_drop_table.asp
            (note, this also removes the table definition)
            So you want next post.

            1 Reply Last reply
            3
            • V Vineela
              6 Feb 2019, 12:53

              @mrjj no only the table in here which i'm inserting it to

              model2->setTable(QStringLiteral("farmer1"));
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Feb 2019, 12:57 last edited by
              #5

              @Vineela "DELETE FROM farmer1" https://www.w3schools.com/sql/sql_delete.asp

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              V 1 Reply Last reply 6 Feb 2019, 12:59
              4
              • J jsulm
                6 Feb 2019, 12:57

                @Vineela "DELETE FROM farmer1" https://www.w3schools.com/sql/sql_delete.asp

                V Offline
                V Offline
                Vineela
                wrote on 6 Feb 2019, 12:59 last edited by
                #6

                @jsulm , @mrjj how abt not deleting the table but just the data???? Is it possible ?

                J 1 Reply Last reply 6 Feb 2019, 13:02
                0
                • V Vineela
                  6 Feb 2019, 12:59

                  @jsulm , @mrjj how abt not deleting the table but just the data???? Is it possible ?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Feb 2019, 13:02 last edited by
                  #7

                  @Vineela "DELETE FROM farmer1" https://www.w3schools.com/sql/sql_delete.asp

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply 7 Feb 2019, 04:45
                  4
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 6 Feb 2019, 13:30 last edited by
                    #8

                    To delete the table you use DROP TABLE farmer1. As correctly suggested, DELETE FROM farmer1 removes all the data from the table maintaining the structure

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    4
                    • J jsulm
                      6 Feb 2019, 13:02

                      @Vineela "DELETE FROM farmer1" https://www.w3schools.com/sql/sql_delete.asp

                      V Offline
                      V Offline
                      Vineela
                      wrote on 7 Feb 2019, 04:45 last edited by
                      #9

                      @jsulm , @VRonin yes well this worked

                      query.prepare("DELETE from farmer1");
                          query.exec();
                      

                      thank you guys.

                      1 Reply Last reply
                      0

                      1/9

                      6 Feb 2019, 12:15

                      • Login

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