I have the following insertion code for a person table having fields( name,ipaddress ). Both are defined as type varchar.

Qt Code:
  1. QString personTableQstr =
  2. "create table Person (name varchar(40) primary key,ipaddress varchar(40))";
To copy to clipboard, switch view to plain text mode 

I am trying to insert some entries into this table using the following code -

Qt Code:
  1. QSqlQuery nameQuery;
  2. nameQuery.prepare( "SELECT * FROM Person WHERE name=:name" );
  3. nameQuery.bindValue( ":name",tempname1 );
  4. nameQuery.exec();
  5. if( !nameQuery.first() )
  6. {
  7.  
  8. logfile.write( QString("DB does not contain this name ...").toAscii());
  9. QSqlQuery insertionQuery;
  10. insertionQuery.prepare( "INSERT INTO person (name, ipaddress) "
  11. "VALUES (?, ?)");
  12. insertionQuery.bindValue(0, tempname1); //tempname1 contains name
  13. insertionQuery.bindValue(1, tempname2); //tempname2 contains ip address
  14.  
  15. if( !insertionQuery.exec() )
  16. {
  17. logfile.write( QString("\n\nInsertion of new person record failed").toAscii());
  18. }
  19. }
  20. else
  21. {
  22. logfile.write( QString("\n\nName already present in DB").toAscii());
  23.  
  24. }
To copy to clipboard, switch view to plain text mode 

I am testing this code by inserting same value ( for name & ipaddress ) twice.Basically the first time I am trying to insert it enters the if-case. But even the second time it enters the if-case, but insertion fails & prints out "
Insertion of new person record failed
". But logically the first() API of QSqlQuery should return true for the second time insertion.

Is my assumption correct ? How does this API work ?