Results 1 to 4 of 4

Thread: How to insert certain table_1 column value in table_2

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default How to insert certain table_1 column value in table_2

    I am stuck and request help. I have an Sqlite3 table and code with PyQt5 in PyCharm. I want to insert id's latest row of table_1 into a "import" column in table_2 simultaneously when a button is click to make new record for/in table_2. [Fetch table_1 id's latest row number and insert into "import" column along with name1 and name2 (user input) text value in table_2 when a button is click.]

    Table_1:
    CREATE TABLE "table_1" (
    "id" INTEGER UNIQUE,
    "first_name" TEXT,
    "last_name" TEXT,
    PRIMARY KEY("id" AUTOINCREMENT)
    )

    Table_2:
    CREATE TABLE "table_2" (
    "import" INTEGER,
    "name1" TEXT,
    "name2" TEXT
    )

    I have google many website but could not find clue/answer to my problem. There are some code to insert value(s) into another table but they missed out on how to insert into along with capturing the QlineEdit user's input values.

    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    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 insert certain table_1 column value in table_2

    Quote Originally Posted by Lalremruata View Post
    I am stuck and request help. I have an Sqlite3 table and code with PyQt5 in PyCharm. I want to insert id's latest row of table_1 into a "import" column in table_2 simultaneously when a button is click to make new record for/in table_2. [Fetch table_1 id's latest row number and insert into "import" column along with name1 and name2 (user input) text value in table_2 when a button is click.]
    How do you insert the row into table_1? Using a QSqlQuery or QSqlTableModel or something else?

  3. #3
    Join Date
    Jun 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to insert certain table_1 column value in table_2

    By means of QSqlQuery

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    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 insert certain table_1 column value in table_2

    Quote Originally Posted by Lalremruata View Post
    By means of QSqlQuery
    First you should read this and this

    Then your code works something like this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database(); // I am assuming default database connection
    2.  
    3. QSqlQuery qry1;
    4. qry1.prepare("insert into table_1(first_name, last_name) values (:first_name, :last_name)");
    5. QSqlQuery qry2;
    6. qry2.prepare("insert into table_2(import, name1, name2) values (last_insert_rowid(), :name1, :name2)" );
    7.  
    8. if (db.transaction()) {
    9. bool success = false;
    10. qry1.bindValue(":first_name", ui->lineEdit1->text());
    11. qry1.bindValue(":last_name", ui->lineEdit2->text());
    12. if (qry1.exec()) {
    13. qry2.bindValue(":name1", ui->lineEdit3->text());
    14. qry2.bindValue(":name2", ui->lineEdit4->text());
    15. if (qry2.exec()) {
    16. success = true;
    17. }
    18. }
    19. if (success) {
    20. db.commit();
    21. }
    22. else {
    23. // SQL died for some reason
    24. db.rollback();
    25. }
    26. }
    27. else {
    28. // error could not start transaction
    29. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Insert Column into Excel Spreadsheet
    By agconnell in forum Newbie
    Replies: 1
    Last Post: 28th October 2020, 08:31
  2. Replies: 6
    Last Post: 26th July 2019, 01:09
  3. Qtableview insert row and column
    By maarvi in forum Newbie
    Replies: 1
    Last Post: 1st July 2011, 09:26
  4. Replies: 1
    Last Post: 12th April 2011, 23:43
  5. Insert into array column
    By wirasto in forum Newbie
    Replies: 3
    Last Post: 9th November 2009, 15:07

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.