1

Hi I need to delete all rows from QSqlDatabase table, my code looks,

      QString dbName = QDir::currentPath()+"/DB";
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName(dbName);
      if(db.open()){
        QSqlQuery query(db);
        query.prepare("truncate fRecogtable"); // this not working
        //query.prepare("Delete from fRecogtable"); //this working

        if(!query.exec() )
          qDebug() << "deletion failed";
        query.clear();
        db.close();
        }

The issue is the command truncate fRecogtable not deleting the record from table where as Delete from fRecogtable working. I need to use truncate as I have to reset auto incremental fields.

What can be the issue?

Edit:

Finally I manged to get work with

query.prepare("Delete from fRecogtable");
 if(!query.exec() )
    qDebug() << "deletion failed";

 query.clear();
 query.prepare("DELETE FROM SQLITE_SEQUENCE WHERE name='fRecogtable'");
 if(!query.exec() )
     qDebug() << "deletion failed";

Thanks Haris

Haris
  • 13,645
  • 12
  • 90
  • 121

1 Answers1

1

In PyQt this problem solves with query.exec_('TRUNCATE my_table;'), you don't need prepare().

sorbo
  • 11
  • 3