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?
-
@Yunus New columns are not inserted using INSERT INTO, but https://www.w3schools.com/sql/sql_alter.asp
-
@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