Results 1 to 13 of 13

Thread: Trouble with "INSERT" by QSqlTableModel

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

    Default Trouble with "INSERT" by QSqlTableModel

    Qt Code:
    1. /// Добавление серийного номера
    2. bool AddSerialNumber::add()
    3. {
    4. /// Иметь в виду, что длина строки = 9 байт. 9-й байт - конец строки. При генерации файла ключа выкидывать 9-й байт
    5. while(element -> serialName.size() <= 8)
    6. element -> serialName += (QChar)32;
    7.  
    8. int model = selectModelID();
    9.  
    10. model.setTable("DeviceList");
    11. //model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    12. model.select();
    13. int row = model.rowCount();
    14. bool k = model.insertRow(row);
    15. model.setData(model.index(row, 0), element -> serialName);
    16. model.setData(model.index(row, 1), element -> secret);
    17. model.setData(model.index(row, 2), true);
    18. model.setData(model.index(row, 3), modelID);
    19. bool ins = model.submitAll();
    20. string err = model.lastError().text().toStdString();
    21.  
    22. return ins;
    23. }
    To copy to clipboard, switch view to plain text mode 
    This code does not insert record in table. I don't understand - why? That code is executed:
    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. QSqlQuery query;
    9. QString insert_query1 = QString("insert into DeviceList(serialNumber, secretKey, security, modelID) "
    10. "values('%1', '%2', %3, %4)").arg(element -> serialName).arg(element -> secret).arg(true).arg(modelID);
    11. bool ins = query.exec(insert_query1);
    12.  
    13. return ins;
    14. }
    To copy to clipboard, switch view to plain text mode 

    What is wrong? Why do I can't use QSqlTableModel?

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    there is a bug in Qt that might be related to this: http://trolltech.com/developer/task-...ntry&id=227468
    (check your debug output for "No fields to update.")

    Try if your code works with Qt 4.4.0

    HTH

  3. The following user says thank you to caduel for this useful post:

    AD (7th November 2008)

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Quote Originally Posted by caduel View Post
    there is a bug in Qt that might be related to this: http://trolltech.com/developer/task-...ntry&id=227468
    (check your debug output for "No fields to update.")

    Try if your code works with Qt 4.4.0

    HTH
    My code works with Qt 4.3.2. Can I use QSqlTableModel for INSERT, UPDATE?

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    My code works with Qt 4.3.2. Can I use QSqlTableModel for INSERT, UPDATE?
    sure.
    (of course, only with a Qt version that is not buggy here. i.e. <= 4.4.0 and hopefully >= 4.4.4)

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Quote Originally Posted by caduel View Post
    sure.
    (of course, only with a Qt version that is not buggy here. i.e. <= 4.4.0 and hopefully >= 4.4.4)
    Where are errors? Why my code doesn't work i.e. submitAll return false for INSERT. Why?

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    possibilites
    i) the bug i mentioned (or some other bug)
    ii) did you check the columns match the order matches your inserts?

    Is there any debug output that you get?
    What error is returned (if any)?

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Error
    [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
    which geted by
    Qt Code:
    1. model.lastError()
    To copy to clipboard, switch view to plain text mode 

    How can I check the order? I saw mentioned order by Microsoft Sql Express Manager

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

    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

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

    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

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    Au! Help, please!

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

    Default Re: Trouble with "INSERT" by QSqlTableModel

    any error message / debug output that could help us?

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

    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!

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.