Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Clone data from one QSqlTableModel to new QSqlTableModel

    General and Desktop
    3
    4
    1042
    Loading More Posts
    • 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 Vaez
      Hasan Vaez last edited by 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,

      JonB 1 Reply Last reply Reply Quote 0
      • V
        VRonin last edited by

        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 Reply Quote 1
        • Hasan Vaez
          Hasan Vaez last edited by Hasan Vaez

          @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 Reply Quote 0
          • JonB
            JonB @Hasan Vaez last edited by JonB

            @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 Reply Quote 0
            • First post
              Last post