Results 1 to 11 of 11

Thread: INSERT query with MySQL problem

  1. #1
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    5

    Default INSERT query with MySQL problem

    hey everybody
    im a bit of a newb to Qt so keep that in mind
    I'm trying to make an application that sends data to a MySQL database
    i can successfully send data using the INSERT INTO statement, but it does not compile when i try to use variables for the data values
    the variables i am using are strings and the fields i am trying to insert them into are all varchar(20)
    here is my code (which i found by searching this forum):
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("INSERT INTO TradeData (symbol,price,size,time) "
    3. "VALUES ( '"+symbol+"' , '"+price+"' , '"+size+"' , '"+timestamp+"' )");
    To copy to clipboard, switch view to plain text mode 
    attached is a screencap of my compile error

    thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: INSERT query with MySQL problem

    You pass std::string to QSqlQuery::exec(), but it expects a QString.


    P.S. Don't construct your queries like that, use QSqlQuery::bindValue().

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

    timmyg (20th March 2008)

  4. #3
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    5

    Default Re: INSERT query with MySQL problem

    Quote Originally Posted by jacek View Post
    You pass std::string to QSqlQuery::exec(), but it expects a QString.

    P.S. Don't construct your queries like that, use QSqlQuery::bindValue().
    ok
    how do i do the bindvalue with variables?

  5. #4
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: INSERT query with MySQL problem

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO TradeData (symbol,price,size,time) VALUES ( :symbol, :price, :size, :timestamp)");
    3. query.bindValue(":symbol",symbol);
    4. query.bindValue(":price",price);
    5. query.bindValue(":size",size);
    6. query.bindValue(":timestamp",timestamp);
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 

    It replaces corresponding strings with the value you want (formatting it accordingly to the database formats)

    Greetings,
    Raccoon29
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. The following user says thank you to Raccoon29 for this useful post:

    timmyg (20th March 2008)

  7. #5
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    5

    Default Re: INSERT query with MySQL problem

    thats exactly what i tried and i got compile error
    Qt Code:
    1. error: no matching function for call to 'QSqlQuery::bindValue(const char[8], std::string&)'
    To copy to clipboard, switch view to plain text mode 
    is this also because im using strings instead of Qstrings?
    how do i convert a string to a Qstring?

    thanks

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: INSERT query with MySQL problem

    Quote Originally Posted by timmyg View Post
    how do i convert a string to a Qstring?
    You might want to take a look at QString docs. Hint: search for "std::string" or "StdString".
    J-P Nurmi

  9. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: INSERT query with MySQL problem

    Yes, the problem is exactly that std::string.
    I never tried with std::string, but usually it is very easy to convert to QString:
    Qt Code:
    1. QString var=other_string;
    To copy to clipboard, switch view to plain text mode 
    so try putting your std::string in QString variables and try putting those in the bindValue, then let us know how it goes.
    Last edited by Raccoon29; 20th March 2008 at 16:19. Reason: spelling error
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  10. The following user says thank you to Raccoon29 for this useful post:

    timmyg (20th March 2008)

  11. #8
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    5

    Default Re: INSERT query with MySQL problem

    Quote Originally Posted by Raccoon29 View Post
    Yes, the problem is exactly that std::string.
    I never tried with std::string, but usually it is very easy to convert to QString:
    Qt Code:
    1. QString var=other_string;
    To copy to clipboard, switch view to plain text mode 
    so try putting your std::string in QString variables and try putting those in the bindValue, then let us know how it goes.
    i get compile error:
    Qt Code:
    1. error: conversion from 'std::string' to non-scalar type 'QString' requested
    To copy to clipboard, switch view to plain text mode 

    im going to look into the QString documentation like jpn suggested and see if i can find anything

  12. #9
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    5

    Default Re: INSERT query with MySQL problem

    i got it to work by doing this
    Qt Code:
    1. QString var = stdstringvar.c_str();
    To copy to clipboard, switch view to plain text mode 

    thanks for the help

  13. #10
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: INSERT query with MySQL problem

    IMHO the Qt one is a great documentation, most of answers are in there; every documentation should learn by this one.

    Glad you found your solution
    Greetings
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  14. #11
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: INSERT query with MySQL problem

    This is the way to convert it correct:
    QString::fromStdString(your_std_string);

Similar Threads

  1. qt4.3.1 mysql problem
    By twells55555 in forum Qt Programming
    Replies: 5
    Last Post: 8th October 2007, 21:26
  2. problem with plugin mysql
    By jose.va18 in forum Installation and Deployment
    Replies: 2
    Last Post: 19th September 2007, 12:12
  3. qt and mysql encoding problem
    By ferasodh in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2007, 09:48
  4. Mysql query question
    By twells55555 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:41
  5. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20

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.