Results 1 to 2 of 2

Thread: Submitting QSqlRecord to MySQL database

  1. #1
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Submitting QSqlRecord to MySQL database

    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.

  2. #2
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Submitting QSqlRecord to MySQL database

    Anyone? I really would like to know.

Similar Threads

  1. problem creating a mysql database
    By TonyB in forum Qt Programming
    Replies: 10
    Last Post: 23rd July 2010, 15:39
  2. Accessing xampp mysql database
    By synack in forum Newbie
    Replies: 8
    Last Post: 19th March 2009, 09:08
  3. Putting and getting images from MySQL database?
    By allensr in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2007, 21:47
  4. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  5. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50

Tags for this Thread

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.