Results 1 to 9 of 9

Thread: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    I am using the following code: but it fails. Kindly help on this:

    Qt Code:
    1. bool ok=false;
    2.  
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("screengrabber");
    6. db.setUserName("root");
    7. db.setPassword("1");
    8. ok = db.open();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    What is the result of QSqlDatabase::drivers function call ?

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    It gives QSQLLITE,QODBC3, QODBC

  4. #4
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    That means you do not have MYSQL driver plug-in. See this thread ... or search on QtCentre other threads related to this problem ...

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    Connection Succeeds, But inserting fails. Am using the following code:
    Qt Code:
    1. QFile file("D:\\image1.jpeg");
    2. file.open(QIODevice::ReadOnly);
    3. file.seek(0);
    4. buf=file.read(250000);
    5.  
    6. QSqlQuery query;
    7. query.prepare("INSERT INTO log VALUES(grab_date='2011-04-26 15:55:09',ip_address='172.16.0.51',image=:val);");
    8. query.bindValue ( ":val", buf);
    9. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Here image is the BLOB field in the database.

  6. #6
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    If you write prepare statement as: INSERT INTO log(grab_date, ip_address, image) VALUES ('2011-04-26 15:55:09','172.16.0.51',:val) did it work ?

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    I have tried with both the methods, still fails

  8. #8
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    What is the return of last error ?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to connect QT with MYSQL and perform opeartions like Insert,Delete,Update?

    Quote Originally Posted by Gokulnathvc View Post
    Connection Succeeds, But inserting fails.
    Did you manage to download MySQL, build the Qt MySQL plugin, and make it work in the twenty minutes between posts 4 and 5?

    You say this fails:
    Am using the following code:
    Qt Code:
    1. QFile file("D:\\image1.jpeg");
    2. file.open(QIODevice::ReadOnly);
    3. file.seek(0);
    4. buf=file.read(250000);
    5.  
    6. QSqlQuery query;
    7. query.prepare("INSERT INTO log VALUES(grab_date='2011-04-26 15:55:09',ip_address='172.16.0.51',image=:val);");
    8. query.bindValue ( ":val", buf);
    9. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Here image is the BLOB field in the database.
    What about it fails?
    Does a row get created without data in the blob column, is the blob data incomplete, or does no row get created?
    What does QSqlQuery::lastError() tell you after each line? Cincirin identifies the really obvious invalid SQL problem but you say it still doesn't work.

    Taking a step back: are you sure the file was opened successfully? You don't check. Are you sure the file had data to read and that it ended up in the QByteArray? Are you sure that 250000 bytes is large enough?

    Unless you get into the habit of checking error return values, warning and messages you will always be thrashing about guessing.

Similar Threads

  1. Replies: 15
    Last Post: 8th June 2013, 06:15
  2. Insert/Delete points over image
    By sergio87 in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2011, 07:37
  3. How to know not insert or delete query?
    By ramazangirgin in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 13:43
  4. i need to know insert and delete methods in qsql for phone book application
    By gaurav purohit in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 15th September 2009, 14:04
  5. MYSQL insert
    By allensr in forum Newbie
    Replies: 4
    Last Post: 14th August 2006, 16:55

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
  •  
Qt is a trademark of The Qt Company.