Results 1 to 17 of 17

Thread: insert data to database

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: insert data to database


  2. The following user says thank you to ChrisW67 for this useful post:

    vanduongbk (25th July 2013)

  3. #2
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    hello
    how i can create many table in database ,sample as db1_table ,db2_table ....
    and how i can write a insert function to each dbn_table
    i use qtablewidget ,how when a record was insert to db ,it is also simultaneously display on qtablewidget
    plz me ,thank

  4. #3
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    hi ChrisW67
    i have write a code in result form as below to display the data from a database but when compile ,it appear error:
    C2248: 'QTableWidget::setModel' : cannot access private member declared in class 'QTableWidget'
    see declaration of 'QTableWidget::setModel'
    see declaration of 'QTableWidget'
    Qt Code:
    1. model->setQuery("SELECT start_time,description,finish_time FROM db_table");
    2. model->setHeaderData(0,Qt::Horizontal,tr("start_time"));
    3. model->setHeaderData(1,Qt::Horizontal,tr("description"));
    4. model->setHeaderData(1,Qt::Horizontal,tr("finish_time"));
    5.  
    6. ui->tableWidget_1->setModel(model);
    7. ui->tableWidget_1->show();
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: insert data to database

    Because you want use QTableView instead of QTableWidget.

  6. #5
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    hi nix
    this mean that i can not use QTableWidget for display data ,only can use QTableView ?

  7. #6
    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: insert data to database

    If you want use a table with a model use QTableView if you want to insert your data by yourself use QTableWidget. Read the doc about those two classes and about Model/View programming

  8. #7
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    hi nix
    how i can call data from database in mainform and display this database on result form

  9. #8
    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: insert data to database

    Your code using a model was not so far try this
    http://doc.qt.io/qt-4.8/qsqltablemodel.html/#details

  10. #9
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    my problem is solve
    thank you
    Last edited by vanduongbk; 26th July 2013 at 10:12.

  11. #10
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    hello
    i have record to database and display well on qtable
    but i have a question how the lastest record data to database is the first display on table

  12. #11
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: insert data to database

    Use order by clause
    Qt Code:
    1. model->setQuery("SELECT start_time,description,finish_time FROM db_table [B]order by id desc[/B]");
    To copy to clipboard, switch view to plain text mode 

    hope it helps,
    bala

  13. #12
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: insert data to database

    thank you for your reply
    i have question ,can you help me
    i want to write a below code, but I do not know how to coding them,plz help me
    Qt Code:
    1. ............
    2. if(warning == 0)
    3. {
    4. warning=1;
    5. start_time=getCurrentDateTime(); //i have write a function to get time and i call it here
    6. insert_to_db(start_time, "high value", "...");
    7. current_id= ???????
    8.  
    9. }
    10.  
    11. }
    12. else
    13. {
    14. if(warning==1)
    15. {
    16. warning=0;
    17. end_time=getCurrentDateTime();
    18. update_to_db(end_time,current_id);//i want to update only update end time at above record where ID is ID at insert_to_db(start_time, "high value", "...");
    19. }
    To copy to clipboard, switch view to plain text mode 

    and this is a update function that i write
    Qt Code:
    1. void class::update_to_db(QString finish_time,int id)
    2. {
    3. QSqlQuery query;
    4. query.prepare("update db_table set finish_time= :finish_time" "when id= :id");
    5. query.bindValue(":id",id);
    6. query.bindValue(":finish_time",finish_time);
    7. if(!query.exec())
    8. {
    9. throw QSqlError();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by vanduongbk; 6th August 2013 at 05:41.

Similar Threads

  1. Insert values into database table
    By l0ner in forum Newbie
    Replies: 3
    Last Post: 20th June 2011, 19:03
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  3. SQLite sometimes doens't INSERT into database
    By cevou in forum Qt Programming
    Replies: 5
    Last Post: 30th October 2009, 08:10
  4. Cancelling a pending database insert
    By innerhippy in forum Qt Programming
    Replies: 3
    Last Post: 30th October 2008, 08:53
  5. how to insert an ' in a database
    By jh in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 02:47

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.