Results 1 to 13 of 13

Thread: Trouble with "INSERT" by QSqlTableModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Trouble with "INSERT" by QSqlTableModel

    looks like the order of the fields is not as you expected (seems like column 1 is "security"; you can check by displaying the QSqlTableModel in a QTableView).

    How about this approach:
    populate (access columns by name) a QSqlRecord (you get an empty one with record()) and insert it with QSqlTableModel::insertRecord()
    This way your code does not depend on the order of the columns in the table.

    HTH

  2. #2
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Quote Originally Posted by caduel View Post
    looks like the order of the fields is not as you expected (seems like column 1 is "security"; you can check by displaying the QSqlTableModel in a QTableView).

    How about this approach:
    populate (access columns by name) a QSqlRecord (you get an empty one with record()) and insert it with QSqlTableModel::insertRecord()
    This way your code does not depend on the order of the columns in the table.

    HTH
    Qt Code:
    1. // Добавление серийного номера
    2. bool AddSerialNumber::add()
    3. {
    4. while(element -> serialName.size() <= 8)
    5. element -> serialName += (QChar)32;
    6.  
    7. int modelID = selectModelID();
    8. QSqlTableModel model;model.setTable("DeviceList");
    9. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    10. model.select();
    11. QSqlRecord record = model.record( );
    12. record.setValue("serialNumber", element -> serialName);
    13. record.setValue("secretKey", element -> secret );
    14. record.setValue("security", true);
    15. record.setValue( "modelID", modelID);
    16. model.insertRecord(-1, record);
    17. bool ins = model.submitAll( );
    18.  
    19. return ins;
    20. }
    To copy to clipboard, switch view to plain text mode 

    This code doesn't works too. submitAll return false

  3. #3
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Au! Help, please!

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Trouble with "INSERT" by QSqlTableModel

    any error message / debug output that could help us?

  5. #5
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Trouble with "INSERT" by QSqlTableModel

    This error is:
    [Microsoft][SQL Native Client][SQL Server]Operand type clash: text is incompatible with bit [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared. QODBC3: Unable to execute statement
    ODBC driver work right. Intructions UPDATE, SELECT works, INSERT with QSqlQuery works!

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Trouble with "INSERT" by QSqlTableModel

    i) dump the QSqlRecord to stderr, check esp. the QVariant::Types of the fields
    ii) if those do not match your db: fix the setValue calls to the record to be inserted
    iii) assuming those are correct: file a bug report to the Trolls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.