Results 1 to 3 of 3

Thread: how to add data Into database using a dialog

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default how to add data Into database using a dialog

    hello everyone i am currently implementing a gui app, the problem is i cant quite figure out how to insert data into a database using values from a dialog NOTE i want the user to enter the values in the dialog and then those values are inserted into the database e.g, can anyone please post me a snippet of how to do this. please i will appreciate very much

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to add data Into database using a dialog

    Hi,

    I'm assuming you are sub-classing a QDialog its has some widgets (for example lineEdits) and the insertion happens once the user close the dialog.

    Lets say for example that mydialog has two lines edits: Code (lineEdit) and Name (lineEdit_2)

    declare two private members in mydialog:

    in the .h
    Qt Code:
    1. ...
    2. private:
    3. QString m_code;
    4. QString m_name
    To copy to clipboard, switch view to plain text mode 

    declare two public functions to get the name and code:

    in the .h
    Qt Code:
    1. public:
    2. QString getCode(){return m_code;}
    3. QString getName(){return m_name;}
    To copy to clipboard, switch view to plain text mode 

    I guessing you have a "OK" or "Close" button, so move the data from the edits to the private members when closing

    in the .cpp
    Qt Code:
    1. m_code = ui->lineEdit->text();
    2. m_name = ui->lineEdit_2->text();
    To copy to clipboard, switch view to plain text mode 

    Now another window is the one calling the dialog right?

    Qt Code:
    1. //Creates the dialog, execute it and wait until close
    2. mydialog mydlg;
    3. mydlg.exec();
    4.  
    5. //Insert the data. I'm old fashion so I prefer to write SQL code and the execute it....but QT has many classes to handle inserts in a more automatic way.
    6. QString sql;
    7. QSqlQuery qry(mydb); //mydb has to be created previously. Is a QSQLDatabase... See help if you don't know how to open and database
    8.  
    9. sql = "INSERT INTO mytable (code,name) VALUES ('" + mydlg.getCode() + "','" + mydlg.getName() + "')"; //Creates the sql insert
    10.  
    11. qry.exec(sql); //Executes the insert
    To copy to clipboard, switch view to plain text mode 

    And that's it.

    Good luck.

  3. #3
    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: how to add data Into database using a dialog

    Quote Originally Posted by qlands
    Qt Code:
    1. sql = "INSERT INTO mytable (code,name) VALUES ('" + mydlg.getCode() + "','" + mydlg.getName() + "')"; //Creates the sql insert
    To copy to clipboard, switch view to plain text mode 
    It also creates reliability and security issues if you get into the habit of building queries this way. If one of the user's name entries was "Miles O'Brien (Star Trek)" (without the enclosing quotes) the INSERT will fail. If the INSERT was a DELETE the consequences could be dire:
    Qt Code:
    1. sql = "DELETE FROM mytable WHERE code = '" + mydlg.getCode() +"'";
    To copy to clipboard, switch view to plain text mode 
    seems safe enough until someone exploits the weakness by typing "x' OR code is not null; -- " in as the code, making the query:
    Qt Code:
    1. DELETE FROM mytable WHERE code = 'x' OR code is not null;-- '
    To copy to clipboard, switch view to plain text mode 
    and trashes much data (on most RDMS systems). Might not matter on your single-user desktop app, but it matters greatly in the broader world.

    QSqlQuery provides Approaches to Binding Values that ensure user input is safely handled: please use them.

Similar Threads

  1. Passing data to dialog via dialog or a method
    By mtnbiker66 in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2012, 02:39
  2. getting data from a Dialog
    By graciano in forum Newbie
    Replies: 6
    Last Post: 21st August 2009, 21:37
  3. Change database data in the QTabelView
    By sophister in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2009, 17:40
  4. Replies: 2
    Last Post: 8th August 2008, 02:54
  5. How to get data from .mdb database?
    By whoops.slo in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 22:27

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.