Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML SQLite database inserting new columns to database
Forum Updated to NodeBB v4.3 + New Features

QML SQLite database inserting new columns to database

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • YunusY Offline
    YunusY Offline
    Yunus
    wrote on last edited by
    #1

    Hi Qt Forum! I am trying to use SQLite database by taking reference this link. I dont want to paste all the code because its exactly same and working well.

    My problem is this example is prepared for 3 columns and I want to make it 4 or more columns but when I tried to insert new columns(I added the same type of codes in cpp side and also qml side) and click add button. It gives me the warning:

    error insert into NameTable
    " Parameter count mismatch"

    How can I add new columns to this example?

    jsulmJ 1 Reply Last reply
    0
    • YunusY Yunus

      Hi Qt Forum! I am trying to use SQLite database by taking reference this link. I dont want to paste all the code because its exactly same and working well.

      My problem is this example is prepared for 3 columns and I want to make it 4 or more columns but when I tried to insert new columns(I added the same type of codes in cpp side and also qml side) and click add button. It gives me the warning:

      error insert into NameTable
      " Parameter count mismatch"

      How can I add new columns to this example?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Yunus New columns are not inserted using INSERT INTO, but https://www.w3schools.com/sql/sql_alter.asp

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • YunusY Offline
        YunusY Offline
        Yunus
        wrote on last edited by
        #3

        @jsulm Thanks for quick reply. I will check the document you shared but can you guide me more specifically because I am still a beginner.

        jsulmJ 1 Reply Last reply
        0
        • YunusY Yunus

          @jsulm Thanks for quick reply. I will check the document you shared but can you guide me more specifically because I am still a beginner.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Yunus Well, it is shown in the link I posted:

          ALTER TABLE table_name
          ADD column_name datatype;
          

          What exact support do you need?
          Keep in mind that you can simply create the tables with all needed columns.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • YunusY Offline
            YunusY Offline
            Yunus
            wrote on last edited by Yunus
            #5

            @jsulm I mean where I should add these piece of code. Because in this example there is no method similar to it

            jsulmJ 1 Reply Last reply
            0
            • YunusY Yunus

              @jsulm I mean where I should add these piece of code. Because in this example there is no method similar to it

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Yunus It's a SQL query, so you do it in the same way you do with your other queries in your Qt app.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • YunusY Offline
                YunusY Offline
                Yunus
                wrote on last edited by Yunus
                #7

                @jsulm In my example, I think its doing it in createTable() function but how the way I can use the code document suggested. I still couldnt understand how the example adding the columns

                jsulmJ 1 Reply Last reply
                0
                • YunusY Yunus

                  @jsulm In my example, I think its doing it in createTable() function but how the way I can use the code document suggested. I still couldnt understand how the example adding the columns

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @Yunus This is how a table is created in the example you posted:

                      if(!query.exec( "CREATE TABLE " TABLE " ("
                                              "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                                              TABLE_FNAME     " VARCHAR(255)    NOT NULL,"
                                              TABLE_SNAME     " VARCHAR(255)    NOT NULL,"
                                              TABLE_NIK       " VARCHAR(255)    NOT NULL"
                                          " )"
                                      )){
                  

                  If you want to add one more column to this table then simply change that query:

                      if(!query.exec( "CREATE TABLE " TABLE " ("
                                              "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                                              TABLE_FNAME     " VARCHAR(255)    NOT NULL,"
                                              TABLE_SNAME     " VARCHAR(255)    NOT NULL,"
                                              TABLE_NIK       " VARCHAR(255)    NOT NULL",
                                              " my_own_column VARCHAR(255) NOT NULL" 
                                          " )"
                                      )){
                  

                  Adjust this line as needed:

                  "my_own_column VARCHAR(255) NOT NULL" 
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • YunusY Offline
                    YunusY Offline
                    Yunus
                    wrote on last edited by Yunus
                    #9

                    @jsulm This was what I tried also. I finally found the problem. I have forgotten to add my column to prepare part which I commented below:

                    bool DataBase::inserIntoTable(const QVariantList &data)
                    {
                        QSqlQuery query;
                    
                        query.prepare("INSERT INTO " TABLE " ( " TABLE_FNAME ", "
                                                                 TABLE_SNAME ", "
                                                                 TABLE_NIK ", "
                                                                 TABLE_MATCH  " ) "
                                      "VALUES (:FName, :SName, :Nik, :Matching)"); // I have forgotten to add my new column to end of  this line
                        query.bindValue(":FName",       data[0].toString());
                        query.bindValue(":SName",       data[1].toString());
                        query.bindValue(":Nik",         data[2].toString());
                        query.bindValue(":Matching",    data[3].toString());
                    
                    
                    
                        if(!query.exec()){
                            qDebug() << "error insert into " << TABLE;
                            qDebug() << query.lastError().text();
                            return false;
                        } else {
                            return true;
                        }
                        return false;
                    }
                    

                    Thank you so much also @jsulm

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved