Results 1 to 17 of 17

Thread: insert data to database

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

    Default insert data to database

    hi everyone,
    i have init a database and i want to insert data to this database ,i write a code as below, when compiler without any error but can not insert any data
    please help me
    Qt Code:
    1. void MainForm::creat_db()
    2. {
    3. db = QSqlDatabase::addDatabase("QSQLITE");
    4. #ifdef Q_OS_LINUX
    5. // store database file into user home folder in Linux
    6. QString path(QDir::home().path());
    7. path.append(QDir::separator()).append("my.db.sqlite");
    8. path = QDir::toNativeSeparators(path);
    9. db.setDatabaseName(path);
    10. #else
    11. // file exists in the application private folder
    12. db.setDatabaseName("my.db.sqlite");
    13. #endif
    14. // Open databasee
    15. if(!db.open())
    16. {
    17. QMessageBox::warning(0, QObject::tr("database error"),db.lastError().text());
    18. }
    19.  
    20. QSqlQuery query;
    21. query.exec("create table db_table "
    22. "(id integer primary key, "
    23. "start_time varchar(20), "
    24. "decription varchar(30), "
    25. "finish_time varchar(20))");
    26. }
    27.  
    28. int MainForm::insert_to_db(QString start_time, QString description, QString finish_time)
    29. {
    30. int newId = -1;
    31. bool ret = false;
    32. if (db.isOpen())
    33. {
    34. // NULL = is the keyword for the autoincrement to generate next value
    35. QSqlQuery query;
    36. ret = query.exec(QString("insert into db_table(NULL,'%1','%2','%3')")
    37. .arg(start_time).arg(description).arg(finish_time));
    38. // Get database given autoincrement value
    39. if (ret)
    40. {
    41. newId = query.lastInsertId().toInt();
    42. }
    43. }
    44. return newId;
    45. }
    To copy to clipboard, switch view to plain text mode 

    and I call them in mainform
    Qt Code:
    1. MainForm::MainForm(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainForm)
    4. {
    5. ui-> setupUi(this);
    6. creat_db();
    7. insert_to_db("10:10","too high","10:12"); //only test
    8. }
    9. ...........
    To copy to clipboard, switch view to plain text mode 

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

    First you should check exec() return to check the status. Then this is sql command I guess you should have a ";" at the end of the string.

    To be sure, check the return value of every calls to exec() and if it's not ok use lastError() method to get more information about the error you got.

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

    Default Re: insert data to database

    thank nix
    can you edit above code and write a demo for it

  4. #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

    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.

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

    vanduongbk (24th July 2013)

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

    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

  7. #6
    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


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

    vanduongbk (25th July 2013)

  9. #7
    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

  10. #8
    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 

  11. #9
    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.

  12. #10
    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 ?

  13. #11
    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

  14. #12
    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

  15. #13
    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

  16. #14
    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.

  17. #15
    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

  18. #16
    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

  19. #17
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.