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
QtWS25 Last Chance

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.
  • VineelaV Offline
    VineelaV Offline
    Vineela
    wrote on last edited by Vineela
    #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..!!!

    mrjjM 1 Reply Last reply
    0
    • jsulmJ jsulm

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

      VineelaV Offline
      VineelaV Offline
      Vineela
      wrote on 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
      • VineelaV Vineela

        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..!!!

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

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

        VineelaV 1 Reply Last reply
        0
        • mrjjM mrjj

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

          VineelaV Offline
          VineelaV Offline
          Vineela
          wrote on last edited by
          #3

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

          model2->setTable(QStringLiteral("farmer1"));
          
          mrjjM jsulmJ 2 Replies Last reply
          0
          • VineelaV Vineela

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

            model2->setTable(QStringLiteral("farmer1"));
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #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
            • VineelaV Vineela

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

              model2->setTable(QStringLiteral("farmer1"));
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

              VineelaV 1 Reply Last reply
              4
              • jsulmJ jsulm

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

                VineelaV Offline
                VineelaV Offline
                Vineela
                wrote on last edited by
                #6

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

                jsulmJ 1 Reply Last reply
                0
                • VineelaV Vineela

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

                  VineelaV 1 Reply Last reply
                  4
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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
                    • jsulmJ jsulm

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

                      VineelaV Offline
                      VineelaV Offline
                      Vineela
                      wrote on 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

                      • Login

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