Results 1 to 14 of 14

Thread: MYSQL Qt connectivity??

  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 MYSQL Qt connectivity??

    Could anybody please provide a good source code which successfully connects MYSQL with Qt and the changes should be updated in the database. A very simple code is quite better..

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MYSQL Qt connectivity??

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  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: MYSQL Qt connectivity??

    I have used the following code to connect to the mysql database, the connection succeeds, but nothing is happened in the database and also no table is created and the rows are not indeed updated. Kindly help me on this issue..

    Qt Code:
    1. QSqlDatabase db= QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("DRIVER={MYSQL ODBC 3.51 Driver};FIL={MYSQL};DBQ=screengrabber");
    3. db.setHostName("localhost");
    4. //db.setConnectOptions("CLIENT_ODBC");
    5. //db.setDatabaseName("screengrabber");
    6. db.setUserName("root");
    7. db.setPassword("1");
    8. ok = db.open();////Here ok is true...
    9.  
    10. QSqlQuery query;
    11. query.exec("create table person (id int primary key, "
    12. "firstname varchar(20), lastname varchar(20))");
    13. query.exec("insert into person values(101, 'Danny', 'Young')");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MYSQL Qt connectivity??

    Check whether query.exec(..) returns true. If it returns false, you can get the error with query.lastError().text()

  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: MYSQL Qt connectivity??

    It gives the following error Message::

    [MYSQL][ODBC 3.51 DRIVER][mysqld-5.0.45-community-nt] No Database selected QODBC: Unable to execute the statement.

  6. #6
    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: MYSQL Qt connectivity??

    I have used the following query which succees in inserting into the table. But how to bind the values instead of directly giving the values as below::
    Qt Code:
    1. bool qrylog=querylog.exec("INSERT INTO service_log (ip_address,date_time, service_status, logged_user) "
    2. "VALUES ('172.16.0.51','2011-07-28 15:55:09',1,'ramachandran')");
    To copy to clipboard, switch view to plain text mode 

    Here in database ip_address is varchar(20), date_time is datetime, service_status is integer and
    logged_user is varchar(30).

    Please help me fixing this issue::
    Last edited by Gokulnathvc; 28th July 2011 at 11:37.

  7. #7
    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: MYSQL Qt connectivity??

    Which bit of the excellent documentation are you having difficulty with?

  8. #8
    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: MYSQL Qt connectivity??

    The entries for each field has been hardcoded and it is not storing the values in a variable and binding the values to it. Could you help me with my previous post??

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: MYSQL Qt connectivity??

    Just open the link from ChrisW67 and look at first example !

  10. #10
    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: MYSQL Qt connectivity??

    Those are examples with hard coded values, it doesnt explain about the variable storing the values and then assigning the variable to the field value..

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: MYSQL Qt connectivity??

    Those values are only examples, bindValue() accepts everything convertible to QVariant, so for example this will work:
    Qt Code:
    1. int number = ...
    2. QString name = ...
    3. QString surname = ...
    4. ...
    5. QSqlQuery query;
    6. query.prepare("INSERT INTO person (id, forename, surname) "
    7. "VALUES (:id, :forename, :surname)");
    8. query.bindValue(":id", number);
    9. query.bindValue(":forename", name);
    10. query.bindValue(":surname", surname);
    11. query.exec();
    To copy to clipboard, switch view to plain text mode 

  12. #12
    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: MYSQL Qt connectivity??

    One row has been created with NULL values and 0 in number field alone is printed where i gave the value for number as 9.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MYSQL Qt connectivity??

    Apparently your code was incorrect.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    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: MYSQL Qt connectivity??

    I have used the following code also: it says unsupported buffer type.

    Qt Code:
    1. QString ipname="gok";
    2. QString datetime="2011-07-28 15:55:09";
    3. QString servstr="1";
    4. QString hostname="gokul";
    5. QSqlQuery querylog(db);
    6. querylog.prepare("INSERT INTO service_log (ip_address,date_time, service_status, logged_user)VALUES (?,?,?,?)");
    7. querylog.bindValue(0,ipname);
    8. querylog.bindValue(1,datetime);
    9. querylog.bindValue(2,serv);
    10. querylog.bindValue(3,hostname);
    11. querylog.exec()
    To copy to clipboard, switch view to plain text mode 


    Added after 20 minutes:


    Qt Code:
    1. QSqlQuery querylog(db);
    2. querylog.exec("INSERT INTO service_log (ip_address,date_time, service_status, logged_user)VALUES (?,?,?,?)");
    3. querylog.bindValue(0,ipname);
    4. querylog.bindValue(1,datetime);
    5. querylog.bindValue(2,serv);
    6. querylog.bindValue(3,hostname);
    7. bool qrylog= querylog.exec();
    To copy to clipboard, switch view to plain text mode 

    This works fine...........
    Last edited by Gokulnathvc; 29th July 2011 at 13:38.

Similar Threads

  1. Microsoft SQL Server Connectivity from Mac
    By deejross in forum Qt Programming
    Replies: 1
    Last Post: 21st October 2011, 09:19
  2. Oracle Connectivity.
    By yesraaj in forum Installation and Deployment
    Replies: 4
    Last Post: 28th February 2007, 11:05
  3. QT and Office Connectivity
    By sarav in forum Newbie
    Replies: 2
    Last Post: 23rd February 2007, 06:16
  4. Database Connectivity
    By yesraaj in forum Newbie
    Replies: 4
    Last Post: 21st February 2007, 13:58
  5. Text box and list box connectivity
    By Rekha in forum Qt Programming
    Replies: 18
    Last Post: 28th July 2006, 12:46

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.