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. Clone data from one QSqlTableModel to new QSqlTableModel
Qt 6.11 is out! See what's new in the release blog

Clone data from one QSqlTableModel to new QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.7k 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.
  • Hasan VaezH Offline
    Hasan VaezH Offline
    Hasan Vaez
    wrote on last edited by Hasan Vaez
    #1

    Hello guys,
    I want to clone or copy from one Table to another one.
    with the following codes they just refer to each other:

    QSqlTableModel * OldModel = new QSqlTableModel(this);
    OldModel->setTable(Program::DataBase::Definitions::TableName);
    OldModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    OldModel->select();

    QSqlTableModel *NewModel = OldModel; (No error but this two object are same object. All refer to one place of memory)

    If you change one, another will change.

    Is there any solution to get a copy of rows/records/

    Please do not suggest a deep copy in a FOR loop.

    @Lifetime-Qt-Champion

    Regards,

    JonBJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Please do not suggest a deep copy in a FOR loop.

      Then use QSqlQuery. It's just down to SQL. something like query->prepare("insert into newTable (select * from OldTable)"); query->exec();


      If, however, you want a deep copy, you can use this library:

      QSqlTableModel *NewModel = new QSqlTableModel(this);
      NewModel ->setTable(Program::DataBase::Definitions::AnotherTableName);
      NewModel ->setEditStrategy(QSqlTableModel::OnManualSubmit);
      NewModel ->select();
      BinaryModelSerialiser modelSerialiser(OldModel);
      QByteArray deepCopyData;
      modelSerialiser.saveModel(&deepCopyData);
      modelSerialiser.setModel(NewModel);
      modelSerialiser.loadModel(deepCopyData);
      

      "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
      1
      • Hasan VaezH Offline
        Hasan VaezH Offline
        Hasan Vaez
        wrote on last edited by Hasan Vaez
        #3

        @VRonin said in Clone data from one QSqlTableModel to new QSqlTableModel:

        this library

        Thank you VRonin for your prompt reply,
        I do not test it yet, but there is a Recursive function with at least 10,000 time repeation. Every calling of function will run a FOR loop inside which load all 10,000 records. (10,000 time ^ 10,000 time) I use TableModel in the beginning of this function! Although this time is nothing for sweet QT(C++), but making query is very expensive. There should be codes just to copy a table to each other.
        If I use a query or a deep copy guess how long it will take!

        1 Reply Last reply
        0
        • Hasan VaezH Hasan Vaez

          Hello guys,
          I want to clone or copy from one Table to another one.
          with the following codes they just refer to each other:

          QSqlTableModel * OldModel = new QSqlTableModel(this);
          OldModel->setTable(Program::DataBase::Definitions::TableName);
          OldModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
          OldModel->select();

          QSqlTableModel *NewModel = OldModel; (No error but this two object are same object. All refer to one place of memory)

          If you change one, another will change.

          Is there any solution to get a copy of rows/records/

          Please do not suggest a deep copy in a FOR loop.

          @Lifetime-Qt-Champion

          Regards,

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Hasan-Vaez

          I'm just going to say it: Given you are using SQL tables, are you sure your solution cannot be server-side SQL instead? You're talking about efficiency, and nothing you do client-side in Qt is going to be anywhere vaguely near what you will achieve server-side... This assumes the destination table is "permanent" at the server-side --- you don't actually say what this copying is for.

          Also, if you stick to client-side copying, I don't know where you get your statements/calculations from:

          FOR loop inside which load all 10,000 records

          If you're copying elements from one table to another manually, it will only be in-memory, there will be no "loading [from database]" if that's what you mean?

          (10,000 time ^ 10,000 time)

          What does this mean? 10K to the power of 10K ?? If you are copying 10K records, you will do 9approx) 10K reads + 10K writes == O(20K).

          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