Parameter Count Mismatch
-
I tried to insert new datas in database and it shows parameter count mismatch. It was similar to the code that I had done recently but didn't worked.
void CreateAccount::on_pushButton_create_clicked()
{QString name,address,password,username,gender; qint64 phone; username=ui->line_username->text(); password=ui->line_password->text(); name=ui->line_name->text(); address=ui->line_address->text(); phone=ui->line_phone->text().toInt(); if(ui->radioButton_M->isChecked()) { gender="M";} if(ui->radioButton_F->isChecked()) { gender="F";} QSqlQuery query;
//query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) values ('"+username+"','"+password+"','"+name+"','"+gender+"','"+address+"',"+phone+")"); (tried this too)
query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?, ?, ?, ?, ? ,?)"); query.addBindValue(username); query.addBindValue(password); query.addBindValue(name); query.addBindValue(gender); query.addBindValue(address); query.addBindValue(phone); if(query.exec()){ QMessageBox::information(this,tr("Save"),tr("Saved"));} else { QMessageBox::critical(this,tr("Error."),query.lastError().text()); }
}
-
@Aromakc Print out https://doc.qt.io/qt-5/qsqlquery.html#executedQuery after executing the query to see how the actual query looks like.
Also, you use different casing in the column list: is this a mistake?
(username,password,Name,Gender,Address,Phone_Number) -
@jsulm (username,password,Name,Gender,Address,Phone_Number) is the name of my columns that I have creaed all being string except Phone_Number
While I add qDebug()<< query.lastQuery(); before if(que
r.exec()) it shows:
"INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?,?,?,?,?,?)" -
-
@KroMignon I have created a database connection login.h
QSqlDatabase mydb; // default connection
void connClose(){
mydb.close();
mydb.removeDatabase(QSqlDatabase::defaultConnection); //closing db and removing any connection
}
bool connOpen()
{
QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
if(!mydb.open()){
qDebug()<<("Failed to locate database!");
return false;
}
else {
qDebug()<<("Connected..."); // qDebug("Connected");
return true;
}
}
i have liked login.h in another createaccount.h and coded
Login conn;I think there is no problem creating two tables in databasename.db
-
@Aromakc I was actually talking about https://doc.qt.io/qt-5/qsqlquery.html#executedQuery NOT lastQuery...
-
@Aromakc said in Parameter Count Mismatch:
These may not be relevant to your current issue, but:mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
As pasted here, this is not at all right! I assume you have
\\
in your actual code, but it shows as\
here since you did not put it inside code tags when pasting?qint64 phone;
phone=ui->line_phone->text().toInt();
all being string except Phone_NumberThis is a bad idea. A phone number is a string of digit characters, not a number/integer.
it shows parameter count mismatch
You should copy & paste the exact error message you receive, in case that gives us a clue.
How do we know your
Users
table has exactly 6 columns?Also, looking at your screenshot, what are those "duplicate connection name" warnings about? Your code should be running without any such warnings.
-
@Aromakc
Ok, try to clean up your query, you're mixing lowercase and capital keywords and don't terminate with a ;query.prepare("INSERT INTO Users (username, password, Name, Gender, Address, Phone_Number) VALUES(?, ?, ?, ?, ? ,?);");
-
-
-
@Aromakc
I never suggested the phone number type would be the cause of your issue. But I am pleased you have altered its type off numeric.@JonB said in Parameter Count Mismatch:
SELECT username, password, Name, Gender, Address, Phone_Number from Users
I suggested you try the above from your Qt program (
query.exec()
). This may help us identify where yourINSERT
problem comes from. -
-
@JonB
I have created "Users" table after creating "Inventory" table in items.db from Sqlite Studio. Using another database manager software I could see Both Tables in items.db but
if(query.exec(Select username........ from Users)) showed Users is unavailable.
I used sqlite3 from terminal and it showed Error: No such Tables: Users
So I deleted Users from Sqlite Studio and entered new Users table from terminal and it Worked.(I will look more into Sqlite Studio as changes made into Inventory form the same app worked.)
Thank you and other contributors.