Clone data from one QSqlTableModel to new QSqlTableModel



  • 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,



  • 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);
    


  • @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!



  • @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).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.