Results 1 to 7 of 7

Thread: QSqlQuery problem (insert once but create two rows!!!)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery problem (insert once but create two rows!!!)

    When I insert a name, create two rows

    Here is insert statement
    Qt Code:
    1. void wmtDB::addEmployee(QString name)
    2. {
    3. QSqlQuery qry;
    4. qry.prepare("INSERT INTO employeestuff (name) VALUES (:name)");
    5. qry.bindValue(":name", name );
    6. if (!qry.exec())
    7. qFatal("Failed to add employee");
    8. }
    To copy to clipboard, switch view to plain text mode 

    and here the call
    Qt Code:
    1. void MainWindow::buttonOnClick()
    2. {
    3. employees.addEmployee(ui->lineEdit->text());
    4. updateEmployees();
    5. }
    To copy to clipboard, switch view to plain text mode 

    if db is empty and i run it one time with parameter "john"
    the "SELECT * FROM nameTable"
    will return
    1 "john"
    2 "john"

    please help me
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QSqlQuery problem (insert once but create two rows!!!)

    Connection between your button and your slot is done two times, so your slots is call twice. There is one connect in your ui add by QtDesigner probably (pass in signal/slot view to see it), and one in the constructor.

  3. #3
    Join Date
    Oct 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem (insert once but create two rows!!!)

    thank you, I fix it

  4. #4
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem (insert once but create two rows!!!)

    For reason of avoiding creating a new thread. Could that line of code "connect(this, SIGNAL(senddata(const QString &)), server, SIGNAL(getdata(const QString &)));" cause the problem, which was described above?

    here is the code, what I have in the server-class:
    Qt Code:
    1. qDebug() << "name: " + name << "staus: " + status << "pass: " + pass;
    2. bool ok;
    3. {
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "db");
    5. db.setHostName(this->dbAddress);
    6. db.setPort(this->dbPort);
    7. db.setDatabaseName(this->dbName);
    8. db.setUserName(this->AdminName);
    9. db.setPassword(this->AdminPass);
    10. ok = db.open();
    11. if(ok)
    12. {
    13. QSqlQuery query("INSERT INTO people(name, status, pass) VALUES('" + name + "', '"
    14. + status + "', '" + pass + "')", db);
    15. query.exec();
    16. query.clear();
    17. }
    18. db.close();
    19. }
    20. QSqlDatabase::removeDatabase("db");
    To copy to clipboard, switch view to plain text mode 

    Output is:

    "name: user" "staus: user" "pass: 12dea96fec20593566ab75692c9949596833adc9"

    In my humble opinion, if I got only line with names, passes and statuses, it doesn't. But still I've got every time two new lines in the database table... Where can be a problem?

  5. #5
    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: QSqlQuery problem (insert once but create two rows!!!)

    If you are afraid that connection has been made multiple times, use Qt::UniqueConnection
    Qt Code:
    1. connect(this, SIGNAL(senddata(const QString &)), server, SIGNAL(getdata(const QString &)),Qt::UniqueConnection);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 11th October 2014, 14:21
  2. [pyqt] Can't insert Nulls Using QSqlQuery.execBatch
    By ftnirp in forum Qt Programming
    Replies: 0
    Last Post: 3rd October 2011, 21:07
  3. qsqlquery insert error
    By fantom in forum Qt Programming
    Replies: 4
    Last Post: 23rd February 2011, 17:15
  4. TableView + SqlQueryModel: can't insert or edit rows
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2008, 21:55
  5. Insert rows...
    By steg90 in forum Qt Programming
    Replies: 13
    Last Post: 18th September 2007, 13: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.