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
    Dec 2008
    Location
    France
    Posts
    93
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanked 23 Times in 22 Posts

    Default Re: insert data to database

    Your insert command was wrong. Try

    Qt Code:
    1. // Add include QSqlQuery, QSqlError, QtDebug
    2. void MainForm::creat_db()
    3. {
    4. db = QSqlDatabase::addDatabase("QSQLITE");
    5. #ifdef Q_OS_LINUX
    6. // store database file into user home folder in Linux
    7. QString path(QDir::home().path());
    8. path.append(QDir::separator()).append("my.db.sqlite");
    9. path = QDir::toNativeSeparators(path);
    10. db.setDatabaseName(path);
    11. #else
    12. // file exists in the application private folder
    13. db.setDatabaseName("my.db.sqlite");
    14. #endif
    15. // Open databasee
    16. if(!db.open())
    17. {
    18. QMessageBox::warning(0, QObject::tr("database error"),db.lastError().text());
    19. }
    20.  
    21. QSqlQuery query;
    22. if ( ! query.exec("create table db_table "
    23. "(id integer primary key, "
    24. "start_time varchar(20), "
    25. "decription varchar(30), "
    26. "finish_time varchar(20));"))
    27. {
    28. qDebug() << query.lastError().text();
    29. }
    30. }
    31.  
    32. int MainForm::insert_to_db(QString start_time, QString description, QString finish_time)
    33. {
    34. int newId = -1;
    35. bool ret = false;
    36. if (db.isOpen())
    37. {
    38. // NULL = is the keyword for the autoincrement to generate next value
    39. QSqlQuery query;
    40. ret = query.exec(QString("insert into db_table(start_time, decription, finish_time) values('%1','%2','%3');")
    41. .arg(start_time).arg(description).arg(finish_time));
    42. // Get database given autoincrement value
    43. if (ret)
    44. {
    45. newId = query.lastInsertId().toInt();
    46. }
    47. else
    48. {
    49. qDebug() << query.lastError().text();
    50. }
    51. }
    52. return newId;
    53. }
    To copy to clipboard, switch view to plain text mode 
    You can replace the qDebug() by messagebox.

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

    vanduongbk (24th July 2013)

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

    Default Re: insert data to database

    thank you nix very much ,my problem was solve
    if i create a result form , and result form is child form of mainform , on mainform have result button to open a result form
    i have measurement and write data to a database in mainform ,
    in result form ,i have creat a qtablewidget to display data, how i can display all data form database to this qtablewidget
    thank for help

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: insert data to database


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

    vanduongbk (25th July 2013)

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

    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

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

    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 

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

    Default Re: insert data to database

    Because you want use QTableView instead of QTableWidget.

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

    Default Re: insert data to database

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

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

    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

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

    Default Re: insert data to database

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

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

    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

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

    Default Re: insert data to database

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

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

    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

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

    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

Similar Threads

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