I want to access a MySQL database and I want to read+write data from+to the database within my Qt/C++ program. For the read write process, I try to use QSqlTableModel, QSqlTableRcord and QSqlDatabase as this is a very pleasant approach without too much of SQL commands which I dislike for the one or other reason (to handle myself). I got a similar approach already running (so the database is running already) but it is cluttered all over. So the simple question is what am I doing wrong within these few lines of example code: Using QT 4.5.x The test database has 3 columns: float x, float y, blob img

Qt Code:
  1. int main(){
  2. QImage img("./some_image.png");
  3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  4. db.setHostName("localhost");
  5. db.setDatabaseName("test");
  6. db.setPort(3306);
  7. db.setUserName("root");
  8. db.setPassword("xxxxxxx");
  9. if (!db.open() )
  10. qDebug("Mising db / unable to open");
  11. else {
  12. qDebug() << "tables::" <<db.tables(); //so I see the table exists and gets detected
  13. model.setTable("test_table");
  14.  
  15. rec.setValue(0,1.0f);
  16. rec.setValue(1,2.0f);
  17.  
  18. QBuffer buffer(&ba);
  19. buffer.open(QIODevice::WriteOnly);
  20. img.save(&buffer, "PNG");
  21. rec.setValue(2,ba);
  22.  
  23. model.insertRecord(0,rec);
  24. if (model.lastError().isVaild())
  25. qDebug() << model.lastError().text();
  26. if (!model.submitAll())
  27. qDebug() << "Submit all did not work";
  28. return 0;
  29. }
To copy to clipboard, switch view to plain text mode 

Thx for any help, I already run from one end of the Qt docs to the other but did not find a solution and I already wasted 5 hours doing that, so I am thankful for any hint (unless you suggest to do it completely different).

Maybe I have a wrong picture of QSqlRecord in my head, plx tell me if so.