Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML SQLite database inserting new columns to database



  • 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?


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

    @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.



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


  • Lifetime Qt Champion

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



  • @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


  • Lifetime Qt Champion

    @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" 
    


  • @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


Log in to reply