Results 1 to 11 of 11

Thread: QSqlquery

  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default QSqlquery

    Hello there,

    I have a little problem with my query.
    Qt Code:
    1. QSqlDatabase testdb= QSqlDatabase::database("testdb") ;
    2. for(int i=0; i < dateList.length();i++)
    3. {
    4. QString buf = dateList.at(i);
    5. buf = buf.left(10);
    6. QString buf2 = contentList.at(i);
    7. QSqlQuery insert1("update mytable"
    8. "set filed1= "+buf2+" "
    9. "where partner in ('4569') "
    10. "and Date= CONVERT (DATETIME,'"+buf+" 00:00:00',120) ",testdb);
    11. bool test = insert1.exec();
    12. if(test)
    13. QMessageBox::information(this, tr("query succesful"),tr("test: %1").arg(QString::number(dateList.length())));
    14. }
    To copy to clipboard, switch view to plain text mode 

    this query have no effect on my db it changes no fileds why??

    Have anybody some idea??

  2. #2
    Join Date
    May 2009
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlquery

    are you sure you are connected to your database ?
    wich database you use ?

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

    codeman (3rd June 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlquery

    Yes I use multiple connections thatswhy the first line in my code. I use mssql 9 and 10

    ... my code is correct I don´t see the changes cause I don´t make a refresh in my table ;o))

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlquery

    If filed1 is character column Yours query must be like :
    Qt Code:
    1. QSqlQuery insert1("update mytable"
    2. "set filed1= '"+buf2+"' "
    3. "where partner in ('4569') "
    4. "and Date= CONVERT (DATETIME,'"+buf+" 00:00:00',120) ",testdb);
    To copy to clipboard, switch view to plain text mode 
    You missed ' characters around buf2

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

    codeman (3rd June 2009)

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

    Default Re: QSqlquery

    and we don't use "in" if there is only one value !

    "where partner in ('4569') " ==> "where partner =4569" better

  8. The following user says thank you to QAmazigh for this useful post:

    codeman (3rd June 2009)

  9. #6
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlquery

    thank youuu

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlquery

    ... and you might want read about QSqlQuery::prepare() and QSqlQuery::bindValue() to avoid possible sql injections.

    (then you could put your query outside the loop and create it only once)

  11. The following user says thank you to Lykurg for this useful post:

    codeman (3rd June 2009)

  12. #8
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlquery

    what do you mean in detail I cannot follow the logic in your statement

    perhaps a snippet might be usefull!

  13. The following user says thank you to LordQt for this useful post:

    codeman (3rd June 2009)

  14. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlquery

    Quote Originally Posted by LordQt View Post
    what do you mean in detail I cannot follow the logic in your statement

    perhaps a snippet might be usefull!
    If you mean me:

    Qt Code:
    1. QSqlDatabase testdb= QSqlDatabase::database("testdb") ;
    2. QSqlQuery insert1;
    3. insert1.prepare("update mytable set filed1= :field1 where partner = 4569 and Date= CONVERT (DATETIME, :date, 120)");
    4. for(int i=0; i < dateList.length();i++)
    5. {
    6. QString buf = dateList.at(i);
    7. insert1.bindValue(":field1", buf); //<- will escape the content safely if it is e.g. "asdf'; TRUNCATE TABLE mytable;"
    8. buf = buf.left(10);
    9. insert1.bindValue(":date", buf+" 00:00:00");
    10. if(!insert1.exec())
    11. QMessageBox::information(this, tr("query succesful"),tr("test: %1").arg(QString::number(dateList.length())));
    12. }
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to Lykurg for this useful post:

    codeman (3rd June 2009)

  16. #10
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlquery

    hmmm ok:

    Qt Code:
    1. QSqlDatabase testdb = QSqlDatabase::database("testDB") ;
    2. QSqlQuery insert1(testdb);
    3. insert1.prepare("update mytable set field1= :field1 where partner = '4568' and Date= CONVERT (DATETIME, :date, 120)");
    4. for(int i=0; i < DateList.length();i++)
    5. {
    6. QString buf = DateList.at(i);
    7. insert1.bindValue(":field1", buf);
    8. buf = buf.left(10);
    9. QString buf2 = List.at(i);
    10. insert1.bindValue(":date", buf2+" 00:00:00");
    11. if(!insert1.exec())
    12. QMessageBox::information(this, tr("query not successful"),tr("Length: %1").arg(QString::number(DateList.length())));
    13. }
    To copy to clipboard, switch view to plain text mode 

    This code from your approach don´t make any changes on my db. It seems there is an error.......

  17. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlquery

    Quote Originally Posted by codeman View Post
    This code from your approach don´t make any changes on my db. It seems there is an error.......
    Then we need to see the structure of your database table and two sample rows. Highly probably that with your query and/or your values in DateList is something wrong. What type is your field1 field?

Similar Threads

  1. Replies: 1
    Last Post: 20th May 2009, 20:36
  2. Replies: 9
    Last Post: 6th May 2009, 10:09
  3. QSqlQuery and QLineEdit
    By GuL in forum Newbie
    Replies: 25
    Last Post: 15th August 2008, 13:40
  4. QSqlQuery error
    By skuda in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 08:43
  5. Problems with QSqlQuery update
    By whoops.slo in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2006, 07:17

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.