title: Count Query Size to see if an insert exists
body: <p>I am using qt creator and I have a database with table student, lesson and enroll. </p>
  
  <pre><code>Student (name, lastname, semester, studentid) 
  Lesson (name,lessonid)
  Enroll (idofstudent,idoflesson,grade)
  </code></pre>
  
  <p>I want the user to have the option to add a grade for every student in separete lessons so i created some QlineEdits connected with a Qbutton that inserts into enroll the id of the student, the id of the lesson and his grade.</p>
  
  <p>Problem is that i want to check if the student id user inserts in qlineedit is in my database. If not the insert shouldnt be done as there isnt any student with that id.</p>
  
  <p>Creation of database:</p>
  
  <pre><code>#include "database.h"
  #include &lt;QVariant&gt;
  #include &lt;QDebug&gt;
  #include &lt;QMessageBox&gt;
  
  Database::Database()
  {
      db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("students.db");
      db.open();
      QSqlQuery q(db);
      q.exec(QString("DROP TABLE student;"));
      q.exec(QString("DROP TABLE lesson;"));
      q.exec(QString("DROP TABLE enroll;"));
  
      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)"));
  
      q.exec(QString("create table if not exists lesson(")+
                          QString("id integer primary key autoincrement,")+
                          QString("lname varchar(1024),")+
                          QString("lessonid integer)"));
  
      q.exec(QString("create table if not exists enroll(")+
                          QString("id integer primary key autoincrement,")+
                          QString("idofstudent integer, idoflesson integer,")+
                          QString("grade double)"));
  }
  </code></pre>
  
  <p>Check if student exists. In some way i think here is the mistake as i dont really know if i count the sqlrows correctly.</p>
  
  <pre><code>int  Database::studentexists(int sid)
  {
   QSqlQuery query(db);
   query.exec("SELECT name FROM student where studentid="+QString::number(sid));
   int numRows;
   numRows = query.size();
   return numRows;
  }
  </code></pre>
  
  <p>My button of insert:</p>
  
  <pre><code>void    MainWindow::addSlot3()
  {
  
      if(studentidEdit-&gt;text().isEmpty() || lessonidEdit-&gt;text().isEmpty() || gradeEdit-&gt;text().isEmpty())
      {
          QMessageBox::critical(this,"Error","Empty fields");
          return;
      }
  
  
      if(mydb-&gt;studentexists(studentidEdit-&gt;text().toInt() == 0))
      {
          QMessageBox::critical(this,"Error","Student doesnt exist");
          return;
      }
  
  
      if(gradeEdit-&gt;text().toDouble() &gt; 10)
      {
          QMessageBox::critical(this,"Error","Check Grade");
          return;
      }
  
      Enroll E(studentidEdit-&gt;text().toInt(),lessonidEdit-&gt;text().toInt(),gradeEdit-&gt;text().toDouble());
      mydb-&gt;insertEnroll(E);
  }
  </code></pre>
  
  <p>i have this mydb->studentexists(studentidEdit->text().toInt()==0) cause i think that studentexists should return "1" if it finds a field with the id user trying to add. So if its "1" the function continues with mydb->insertEnroll(E); Else it throws an error.</p>
  
  <p>Problem is that even if an insert with that id user adds exists it returns the error message " Student doesnt exist".</p>