Results 1 to 5 of 5

Thread: Qsqlquery fails to bindvalues

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    22
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Qsqlquery fails to bindvalues

    Dear all, please see below my code. The query fails to bind the values. Does anybody has an idea?

    Qt Code:
    1. #include "simplechatserver.h"
    2. #include <QBuffer>
    3. #include <QByteArray>
    4. #include <QString>
    5. #include <QTcpSocket>
    6. #include <QStringList>
    7. #include <iostream>
    8. #include <QSqlQuery>
    9. #include <QMessageBox>
    10. #include <QSqlError>
    11.  
    12. SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
    13. {
    14. std::cout << "Message server started" << std::endl;
    15. connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
    16. }
    17.  
    18. SimpleChatServer::~SimpleChatServer()
    19. {
    20. }
    21.  
    22. void SimpleChatServer::addConnection()
    23. {
    24. std::cout <<"new incomming connection" << std::endl;
    25. QTcpSocket* connection = nextPendingConnection();
    26. connections.append(connection);
    27. QBuffer* buffer = new QBuffer(this);
    28. buffer->open(QIODevice::ReadWrite);
    29. buffers.insert(connection, buffer);
    30. connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
    31. connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
    32. }
    33.  
    34. void SimpleChatServer::removeConnection()
    35. {
    36. std::cout << "removing connection "<< std::endl;
    37. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    38. QBuffer* buffer = buffers.take(socket);
    39. buffer->close();
    40. buffer->deleteLater();
    41. connections.removeAll(socket);
    42. socket->deleteLater();
    43. }
    44.  
    45. void SimpleChatServer::receiveMessage()
    46. {
    47. std::cout << "receiving" <<std::endl;
    48. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    49. QBuffer* buffer = buffers.value(socket);
    50.  
    51. // missing some checks for returns values for the sake of simplicity
    52. qint64 bytes = buffer->write(socket->readAll());
    53. // go back as many bytes as we just wrote so that it can be read
    54. buffer->seek(buffer->pos() - bytes);
    55.  
    56. {
    57. line = buffer->readLine();
    58. QString str(line.data());
    59. std::cout << str.toStdString() << std::endl;
    60.  
    61.  
    62. QStringList list = str.split(QRegExp(" "), QString::SkipEmptyParts);
    63.  
    64.  
    65.  
    66. if (list.length() != 5) {
    67. return;
    68. }
    69.  
    70. // Extract device number
    71. QString deviceNumber = list[0];
    72. if (deviceNumber.isEmpty()) {
    73. return;
    74. } else
    75. std::cout<< deviceNumber.toStdString() <<std::endl;
    76. // Extract date
    77. QString date = list[1];
    78. if (date.isEmpty()) {
    79. return;
    80. }
    81. // Extract time
    82. QString timestamp = list[2];
    83. if (timestamp.isEmpty()) {
    84. return;
    85. }
    86. // Extract adherense
    87. QString info = list[3];
    88. if (info.isEmpty()) {
    89. return;
    90. }
    91. // Extract cell_counter
    92. QString counter = list[4];
    93. if (counter.isEmpty()) {
    94. return;
    95. }
    96.  
    97.  
    98.  
    99. QSqlQuery query;
    100. query.prepare("INSERT INTO Adherence VALUES ( :deviceID, :date, :time, :info, :counter)");
    101. query.bindValue(":username", deviceNumber);
    102. query.bindValue(":password", date);
    103. query.bindValue(":firstname", timestamp);
    104. query.bindValue(":lastname", info);
    105. query.bindValue(":counter", counter);
    106.  
    107. if (query.exec() == false)
    108. {
    109. std::cout<< "Could not fill adherence table." <<std::endl;
    110. return;
    111. }
    112.  
    113.  
    114.  
    115. emit emitMessage(str);
    116.  
    117. }
    118. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Qsqlquery fails to bindvalues

    Are you kidding us?
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO Adherence VALUES ( :deviceID, :date, :time, :info, :counter)");
    3. query.bindValue(":username", deviceNumber);
    4. query.bindValue(":password", date);
    5. query.bindValue(":firstname", timestamp);
    6. query.bindValue(":lastname", info);
    7. query.bindValue(":counter", counter);
    To copy to clipboard, switch view to plain text mode 
    If you can't see the error, I don't know how we should help you...

    As for the million dollar question: Is a diviceID the same as an username?

  3. #3
    Join Date
    Feb 2011
    Posts
    22
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: Qsqlquery fails to bindvalues

    I request humble apologies to the great minds.Names corrected.

    Can you be so polite to tell me why i still can not bind the values? Do you have any great idea?

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

    Default Re: Qsqlquery fails to bindvalues

    Can you be so polite to tell me why i still can not bind the values? Do you have any great idea?
    Show the updated code. Arguments to "bindValue()" must exactly match the placeholder strings in "prepare()".

  5. #5
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

    Default Re: Qsqlquery fails to bindvalues

    Lol, change your if for this one and tell us the error:
    Qt Code:
    1. if (query.exec() == false)
    2. {
    3. qDebug () << query.lastError ().text ();
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2011, 12:12
  2. Replies: 1
    Last Post: 16th June 2011, 19:13
  3. Select statemen with bindvalues in Firebird 2 doesn't work
    By corrado1972 in forum Qt Programming
    Replies: 0
    Last Post: 1st September 2010, 15:59
  4. Replies: 14
    Last Post: 16th January 2009, 08:11
  5. Batch update fails with QSqlQuery
    By William Wilson in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2007, 15:36

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.