0

So I am missing something on the code but I cant figure out what this is. I am trying to isert some values from text fields into the database but I get the error of the article name.

Can somebody help? I am posting the isert code and the buttons code. Thanks in advance.

Database.cpp

bool    Database::insertStudent(Student &s)
{
bool success = false;
QSqlQuery query(db);
query.prepare("INSERT INTO student (name,lastname,semester,studentid) VALUES (:name,:lastname,:semester,:studentid)");
query.bindValue(":name", s.getname());
query.bindValue(":lastname",s.getlastname());
query.bindValue(":semester", s.getsemester());
query.bindValue(":studentid",s.getstudentid());
query.exec();
if(query.exec()) success = true;
else
{
        qDebug() << "Database error:  "
                 << query.lastError();
}
  return success;
}

MainWindow.cpp

void    MainWindow::addSlot()
{

    if(nameEdit->text().isEmpty() || lastnameEdit->text().isEmpty() || semesterEdit->text().isEmpty() ||
            idEdit->text().isEmpty())
    {
        QMessageBox::critical(this,"Error","Empty fields");
        return;
    }
    Student S(nameEdit->text(),lastnameEdit->text(),semesterEdit->text().toInt(),idEdit->text().toInt());
    mydb->insertStudent(S);
    reloadTable();
}

Table name is student and it has 4 fields i created with this code.

Database::Database()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("students.db");
    db.open();
    QSqlQuery q(db);
    q.exec(QString("create table if not exists student(")+
                        QString("id integer primary key autoincrement,")+
                        QString("name varchar(1024),lastname varchar(1024),semester integer,")+
                        QString("studentid integer)"));
}

student.cpp

#include "student.h"

Student::Student()
{
    name="";
    lastname="";
    studentid=0;
    semester=0;
}

Student::Student(QString n,QString l,int s,int p)
{
    name=n;
    lastname=l;
    semester=s;
    studentid=p;
}

QString Student::getname()
{
    return name;
}

QString Student::getlastname()
{
    return lastname;
}

int Student::getstudentid()
{
    return studentid;
}

int Student::getsemester()
{
    return semester;
}

QString Student::toString()
{
    return name+","+lastname+","+QString::number(semester)+","+QString::number(studentid);
}
pfoutzop
  • 65
  • 11
  • the error is not an issue as the programm starts correctly. Problem is when i push the button to insert the fields into table i get the error on application output: Database error: QSqlError("", "Parameter count mismatch", "") – pfoutzop Jun 18 '17 at 14:34
  • Another issue is the missing comma before studentid in your create statement. – Alex K. Jun 18 '17 at 14:35
  • That , i didnt notice Alex K. Thanks. The error of insert persists though. – pfoutzop Jun 18 '17 at 14:37
  • complete project is here: ufile.io/bds5t – pfoutzop Jun 18 '17 at 14:44
  • can you post the full error stack? – Onur A. Jun 18 '17 at 15:10
  • @OnurA. The only error i get is on application output: Database error: QSqlError("", "Parameter count mismatch", "") when i press the add button.. How can i get more info about it? – pfoutzop Jun 18 '17 at 15:15
  • Alex K. That was the error exatly! The problem was that after that even the table student was already there and didnt changed at all and not recreated. i deleted the old table and the new is Finally working fine! – pfoutzop Jun 18 '17 at 16:22
  • Alex K. if u want write it as an answer so i can approve it! – pfoutzop Jun 18 '17 at 16:23
  • @foutzos sorry for late reply, good to hear it's working! – Onur A. Jun 18 '17 at 19:46

0 Answers0